[英]PHP strtotime( YYYY-mm last day) a month off error
我在PHP 5.2.12中嘗試以下命令:
print (date('Y-m-d', strtotime('2009-12 last day')));
關於php.net手冊 :
date('m/d/y', strtotime('2009-03 last day')); # 03/31/09
它應該顯示2009年3月的最后一天(2009-03-31)!
我的回報是上個月的最后一天? 為什么? :
2009-11-30
您發布的代碼以您描述的方式失敗; 似乎PHP手冊頁中的描述(SilentGhost提到的只是用戶注釋)是未經驗證的代碼。
如果您需要某個月的最后一天,請嘗試以下方法:
date('Y-m-d', strtotime("2009-12 next month - 1 hour"));
如果您使用的是PHP 5.3,請嘗試使用DateTime類:
<?php
$date = new DateTime("2009-03-01");
$date->modify("last day of previous month");
echo $date->format("m/d/y");
?>
它必須是5.3作為$date->modify("last day of previous month");
在5.2。*或更早版本中不起作用。
我認為你只是錯誤地使用相對格式,這對我有用:
echo date('m/d/y', strtotime('last day of 2009-12'));
來自文檔 :
'last day' ' of'?
- 將日期設置為當前月份的最后一天。 此短語最好與其后的月份名稱一起使用。 -"last day of next month"
date("Ymt", strtotime("201512"));
2015年12月
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.