日期操纵问题


Date manipulation issue

我想得到上一个日历月的开始和结束日期。

所以,现在是2012年7月,我希望函数返回2012年6月1日作为开始,返回2012年六月30日作为结束。

这是我的代码:

$current_month = date("Y-m-01 00:00:00");
$start_month = strtotime(date("Y-m-d", strtotime($current_month) . " -1 month"));
$end_month = strtotime(date("Y-m-d", strtotime($current_month) . " -1 second"));
echo "Start Month: " . date('Y-m-d',$start_month) . "( " . $start_month . ")<br>";
echo "End Month: " . date('Y-m-d',$end_month) . "( " . $end_month . ")<br>";

但它的回声:

Start Month: 2012-07-01( 1341093600)
End Month: 2012-07-01( 1341093600)

知道我做错了什么吗?

回答你发布的代码有什么问题,你只需要把你的圆括号移一点就可以了。:)

<?php
$current_month = date("Y-m-01 00:00:00");
$start_month = strtotime(date("Y-m-d", strtotime($current_month . " -1 month")));
$end_month = strtotime(date("Y-m-d", strtotime($current_month . " -1 day")));
echo "Start Month: " . date('Y-m-d',$start_month) . "( " . $start_month . ")<br>";
echo "End Month: " . date('Y-m-d',$end_month) . "( " . $end_month . ")<br>";
?>

试试这个,(查看strtotime的手册)

$now = time();
$current_month = date("Y-m-01 00:00:00", $now);
$start_month = strtotime("-1 month", $now);
$end_month = strtotime("-1 second", $now);

试试这样的东西:

echo date("Y-m-d", mktime(0,0,0,date('m')-1,1,date('y')));
echo date("Y-m-d", mktime(0,0,0,date('m'),0,date('y')));

您(以及其他答案:X)尝试的内容似乎有点过大。这只是

echo date('Y-m-d', strtotime('first day of last month'));
echo date('Y-m-d', strtotime('last day of last month'));

任意月份

// 3 months in the past
echo date('Y-m-d', strtotime('first day of -3 months'));
echo date('Y-m-d', strtotime('last day of -3 months'));
// 3 months in the future
echo date('Y-m-d', strtotime('first day of +3 months'));
echo date('Y-m-d', strtotime('last day of +3 months'));

阅读更多在手册

echo $firstdate= "01/".date('m')."/".date('Y') ;
 $lastdateofmonth=date('t',date('m'));
 echo $lastdate=$lastdateofmonth."/".date('m')."/".date('Y') ;

$date='2012-06-05 12:00:00';
echo "End = ".date("Y-m-t",strtotime($date)); 
echo "start = ".date("Y-m-01",strtotime($date));