簡體   English   中英

星期四的strtotime錯誤?

[英]strtotime bug on thursday?

首先,我要執行的操作是獲取特定日期后接下來7天的日期。 對於第一組,我希望我的結果是11/1/11至11/7/11。 這是代碼,以及它給我的結果。 這是非常非常奇怪的。

$date = "October 31, 2011";
$nxtMon = strtotime(date("Y-m-d", strtotime($date)) . "monday"); 
$nxtTue = strtotime(date("Y-m-d", strtotime($date)) . "tuesday");
$nxtWed = strtotime(date("Y-m-d", strtotime($date)) . "wednesday"); 
$nxtThu = strtotime(date("Y-m-d", strtotime($date)) . "thursday");
$nxtFri = strtotime(date("Y-m-d", strtotime($date)) . "friday"); 
$nxtSat = strtotime(date("Y-m-d", strtotime($date)) . "saturday");  
$nxtSun = strtotime(date("Y-m-d", strtotime($date)) . "sunday");

它給了我:

2011-10-31    <---    I Want the Next Monday
2011-11-01
2011-11-02
1969-12-31    <---    ???????
2011-11-04
2011-11-05
2011-11-06

因此,我將其更改為:

$date = "October 31, 2011";
$nxtMon = strtotime(date("Y-m-d", strtotime($date)) . "next monday"); 
$nxtTue = strtotime(date("Y-m-d", strtotime($date)) . "next tuesday");
$nxtWed = strtotime(date("Y-m-d", strtotime($date)) . "next wednesday"); 
$nxtThu = strtotime(date("Y-m-d", strtotime($date)) . "next thursday");
$nxtFri = strtotime(date("Y-m-d", strtotime($date)) . "next friday"); 
$nxtSat = strtotime(date("Y-m-d", strtotime($date)) . "next saturday"); 
$nxtSun = strtotime(date("Y-m-d", strtotime($date)) . "next sunday");

它給了我:

2011-11-07    <---  Wow, it works!
2011-11-01    <---  Wow, it works!
2011-11-02    <---  Wow, it works!
2011-11-03    <---  Wow, it works!
2011-11-04    <---  Wow, it works!
2011-11-05    <---  Wow, it works!
2011-11-06    <---  Wow, it works!

現在,我嘗試用等於今天的日期作為日期:

$date = "October 22, 2011";
$nxtMon = strtotime(date("Y-m-d", strtotime($date)) . "next monday"); 
$nxtTue = strtotime(date("Y-m-d", strtotime($date)) . "next tuesday");
$nxtWed = strtotime(date("Y-m-d", strtotime($date)) . "next wednesday"); 
$nxtThu = strtotime(date("Y-m-d", strtotime($date)) . "next thursday");
$nxtFri = strtotime(date("Y-m-d", strtotime($date)) . "next friday"); 
$nxtSat = strtotime(date("Y-m-d", strtotime($date)) . "next saturday"); 
$nxtSun = strtotime(date("Y-m-d", strtotime($date)) . "next sunday");

我得到:

2011-10-24
2011-10-25
2011-10-26
2011-10-27
2011-10-28
2011-10-29
2011-10-23   Hooray, it still works!

我似乎已經想出了如何准確地得到自己想要的東西,但是事實是,在我輸入這個問題之前,它似乎工作不正常。 現在突然如此。 它似乎有時可以工作,而不更改其他時間而不進行任何更改。 第一個結果是1969年的奇異之處,我感覺好像不知道發生了什么。

這看起來像是完成我要嘗試的聲音的好方法嗎?

如果有人知道輸入日期並獲取下7個日期的更好方法,您能幫我嗎? 還是讓我對1969年即將來臨一事不bone? 直到昨天,當我注意到星期四的結果沒有顯示在頁面上時,所有內容似乎都可以正常工作,我對此進行了追溯。 謝謝你的時間。

我認為您需要在星期幾之前留一個空格:

$nxtThu = strtotime(date("Y-m-d", strtotime($date)) . " thursday");

我不知道為什么您在“星期四”和一周中的其他日子會得到不同的結果,但是無論如何插入空格還是有意義的( "2011-10-31 thursday""2011-10-31thursday" "2011-10-31 thursday" "2011-10-31thursday" ) 。

嗯...你不想這樣做嗎?

$dateFrom = strtotime($date);
$nxtMon = strtotime('next monday', $dateFrom);
// ...
$nxtSun = strtotime('next sunday', $dateFrom);

從您所希望的最終結果之上,在我看來,這應該是您需要做的所有事情。 看起來您似乎有點使我復雜化,而且您可能還沒有完全閱讀strtotime()手冊頁 -請注意第二個參數...

您正在要求strtotime解釋一個像2011-10-11next monday一那樣荒唐的字符串。 如您所見,這在某些情況下有效,而在其他情況下則無效。 我不會依賴它。 這更可靠:

strtotime('next monday', strtotime($date))

這會將$date轉換為時間戳,並請求相對於該時間戳的“下一個星期一”。 這應該工作得更好。 或者,進行一些手動計算:

$date  = 'October 31, 2011';
$ts    = strtotime($date);
$day   = date('j', $ts);
$month = date('n', $ts);
$year  = date('Y', $ts);

for ($i = 0; $i < 7; $i++) {
    echo date('Y-m-d', mktime(0, 0, 0, $month, $day + $i, $year)) . PHP_EOL;
}

暫無
暫無

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

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