一些PHP日期格式转换和计算的总结

$t1 = time(); //获取当前时间为 1441865485
$t2 = ‘2015-09-03 10:10:00’;
$t22 = strtotime($t2); //转换字符串格式的时间为UNIX标准时间 1441246200
$t12 = date(‘Y-m-d H:i:s’,$t1); //将UNIX标准时间转换为字符串时间 2015-09-10 14:11:25
$t221 = date(‘Y-m-d H:i:s’,strtotime(‘-1 Day’,$t22)); //前一天时间 2015-09-02 10:10:00
$t222 = date(‘Y-m-d H:i:s’,strtotime(‘+1 Day’,$t22)); //后一天时间 2015-09-04 10:10:00
$t223 = date(‘Y-m-d H:i:s’,strtotime(‘-1 Month’,$t22)); //前一个月时间 2015-08-03 10:10:00
$t224 = date(‘Y-m-d H:i:s’,strtotime(‘+1 Month’,$t22)); //后一个月时间 2015-10-03 10:10:00
$t225 = date(‘Y-m-d H:i:s’,strtotime(‘-1 Year’,$t22)); //前一年时间 2014-09-03 10:10:00

$d1 = date_diff(new DateTime(‘2009-10-12’), new DateTime(‘2009-10-13’))->format(‘%R%a’); //计算两个字符串时间相差的天数。> PHP5.3 有效
$d2 = (strtotime(date(‘Y-m-d’,$t1)) – strtotime(date(‘Y-m-d’,$t22))) / (24*60*60); //计算两个UNIX时间相差的天数。

发表评论

邮箱地址不会被公开。 必填项已用*标注

机器人检查 *

分享我的最新文章标题到这里

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据