簡體   English   中英

第一天!=“本月的第一天”

[英]first day != “first day of this month”

使用“第一天”修改日期時,PHP中有一種非常奇怪的行為。

'first day' ' of'? 設置當前月份的第一天。 http://php.net/manual/de/datetime.formats.relative.php

$currentMonthDate = new DateTime("2014-07-30 10:26:00.000000");
echo $currentMonthDate->format('Ym');

$currentMonthDate->modify('first day');
echo $currentMonthDate->format('Ym');

201407/201407 好的

$currentMonthDate = new DateTime("2014-07-31 10:26:00.000000");
echo $currentMonthDate->format('Ym');

$currentMonthDate->modify('first day');
echo $currentMonthDate->format('Ym');

201407/201408 為什么?

$currentMonthDate = new DateTime("2014-08-01 10:26:00.000000");
echo $currentMonthDate->format('Ym');

$currentMonthDate->modify('first day');
echo $currentMonthDate->format('Ym');

201408/201408 好的

我在運行PHP 5.2的生產服務器上發現了這種行為,所以我認為這是一個古老的錯誤,但它發生在PHP 5.3( http://phptester.net/ )和我們的測試服務器上的5.5。

如果我在PHP 5.2中使用“本月的第一天”,則會出現相同的行為。 在PHP 5.5中,“本月的第一天”按預期工作。

是“第一天” - 行為是一個錯誤嗎? 如何在PHP 5.2中獲得“本月的第一天”而不在stringdate之間進行奇怪的轉換?

看起來這個問題是一個文檔問題。

來自Derick的bug#51096:“第一天”和“最后一天”應為“+1天”和“-1天”。

應該更新文檔以反映這種行為,因為目前“第一天/最后一天”部分說“of”是可選的,否則就是可選的。


UPDATE

文檔現已修復。 那么' of'? 不再是可選的。

'first day of'設置當月'first day of'的日期。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM