ste*_*ped 0 php time datetime date
可能重复:
PHP的最后一天
在PHP中,我试图获得上个月的最后一天.例如,今天是2012年12月11日.所以我试图抓住的日期是11月30日.
这就是我目前正在尝试做的事情:
//this grabs the last daye of this month
$current_month = date('Y-m-t',strtotime('today'));
//this should grab November 30th.
$last_month = date("F, Y",strtotime($current_month." -1 month ")
Run Code Online (Sandbox Code Playgroud)
它取代11月30日,而不是抓住12月1日.似乎"-1个月"只是减去了30天.
我如何正确抓住上个月的最后一天?
$datetime = new DateTime('last day of this month');
echo $datetime->format('F jS');
Run Code Online (Sandbox Code Playgroud)
要么
$datetime = new DateTime();
echo $datetime->format('Y-m-t');
Run Code Online (Sandbox Code Playgroud)
第一个允许您根据需要格式化输出.如果要使用非当月的月份,可以将DateTime构造函数传递给有效的日期格式,然后代码将完成其余的工作.