php教程

超轻量级php框架startmvc

PHP中时间加减函数strtotime用法分析

更新时间:2020-03-19 13:01:20 作者:startmvc
本文实例讲述了PHP中时间加减函数strtotime用法。分享给大家供大家参考,具体如下:时间加

本文实例讲述了PHP中时间加减函数strtotime用法。分享给大家供大家参考,具体如下:

时间加减


<?php
//获取本地 提取年份+1
$date=date("Y-m-d",mktime(0,0,0,date("m") ,date("d"),date("Y")+1));
?>

如果要获取数据库中的时间应该如何处理呢?在PHP文档中找到了一个很好的函数strtotime,可以对时间进行加减:

int strtotime ( string time [, int now] )

返回类型 int

函数可用于时间转换和加减。


<?php
//date()格式化时间返回String类型。
//加一年
$date_year = date('Y-m-d',strtotime("$date + 1 year"));
echo $date_year;
//加一天
$date_tomorrow = date('Y-m-d',strtotime("2009-05-26 + 1 day"));
echo $date_tomorrow;
//加一周
$date_week = date('Y-m-d',strtotime("$date + 1 week"));
echo $date_week;
?>

PS:这里再为大家推荐几款时间及日期相关工具供大家参考使用:

在线日期/天数计算器: http://tools.jb51.net/jisuanqi/date_jisuanqi

在线日期计算器/相差天数计算器: http://tools.jb51.net/jisuanqi/datecalc

在线日期天数差计算器: http://tools.jb51.net/jisuanqi/onlinedatejsq

Unix时间戳(timestamp)转换工具: http://tools.jb51.net/code/unixtime

PHP 时间加减 函数 strtotime