求助:怎么得到日期变量的运算结果?
本帖最后由 AmboLong 于 2011-05-06 14:23 编辑比如:
$now 为日期变量,格式类似于:2011-04-14
php怎么得到$now前一个月的日期,也就是2011-03-14?
now是变量。 :emn54: 回复 1# AmboLong
//获取指定日期的前一个月份
$m="2011-04-05"
$last_m = date( "Y-m-d", mktime(0,0,0,date("m",strtotime($m))-1,date("d",strtotime($m)),date("Y",strtotime($m))) );
其余的时间可以直接在天数后面减掉即可,如,前30天,则
$last_m = date( "Y-m-d", mktime(0,0,0,date("m",strtotime($m)),date("d",strtotime($m))-30,date("Y",strtotime($m))) );
其余以此类推
试一下吧 查一下strtotime echo date('y-m-d',strtotime('-1 month')); 回复 1# AmboLong
核心提示:
2011-05-11
这个是字符串
日期的计算要转换成时间戳(从1970年开始到指定时间的秒数)
在这个基础上做加减法,就容易了。需要熟悉几个函数用来做字符串和时间戳的转换
date/time/mktime/strtotime
页:
[1]