且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

phpstrtotime()对于31日求上个月有问题

更新时间:2022-08-14 19:07:12

PHP自带的strtotime()对于31日求上个月有问题,如下:

<?php
$date = "2012-07-31";
$date_unix = strtotime($date);
$lastmonth = strtotime("last month",$date_unix);//或者strtotime("-1 month",$date_unix);
echo date("Y-m-d H:i:s",$lastmonth)."<br />";//输出:2012-07-01 00:00:00
$last_month_end = strtotime(date("Ym"."01000000",$date_unix)) - 1;//上个月的最后一秒
echo date("Y-m-d H:i:s",$last_month_end);//输出:2012-06-30 23:59:59
?>