2012-03-30 101 views
3

我正在製作每週計劃報告。PHP中的日期函數問題

我在當前日期的基礎上生成了一週。請參閱以下代碼。

<?php 
    $date = mktime(0, 0, 0, date('m'), date('d'), date('Y')); 
    $week = (int)date('W', $date); 
    $year = date('Y'); 

    for($day=1; $day<=7; $day++) 
    { 
     echo date('M dS, Y', strtotime($year."W".$week.$day)); 
    } 
?> 

這工作正常,但是當我嘗試在幾周之間導航時,我遇到了問題。對於第9周,我得到日期01-03-1970。它不會生成2月份的星期日期。

也嘗試與靜態日期03-02-2012但獲得相同的隨機日期。

$week =9; //(int)date('W', $date); 

在另一版本

echo date('N', strtotime('03-09-2012')); 

以上的語句返回1。我不相信這裏的結果。

請讓我知道我應該怎麼做,以幫助我找到方法來獲得給定日期的星期幾。

+0

謝謝大家試圖找到我的解決方案。我感謝你的所有努力。迄今爲止的答案很接近,但不完全是我想要的。特別感謝@kdvy指出我的代碼中的問題並解決我的問題。 – pinaldesai 2012-04-05 06:29:35

回答

1

儘量保持解決方案儘可能接近您當前的代碼。

爲您的1970年問題:

看起來的strtotime需要零領先的週數爲10歲以下的周正常工作,否則輸出到1970年,忽略年份(這似乎是一個bug我?)。儘管支持星期幾和星期幾不在時間的官方文​​檔中。

所以我將代替

$week = (int)date('W', $date); 

有:

$week = str_pad(date('W', $date),2,'0',STR_PAD_LEFT); 

對於下面的代碼:

echo date('N', strtotime('03-09-2012')); 

這是返回1日(星期一)爲的strtotime被解釋03-09-2012英國格式的d-m-Y而不是美國格式的m-d-Y。爲避免混淆,您應該使用ISO日期格式Y-m-d。有關更多信息,請參閱PHP manual on Date Formats

所以,如果你想2012年3月9你可以這樣寫:

echo date('N', strtotime('2012-03-09')); 

我希望這有助於!

+0

這正是我想要知道的克服這個日期問題。非常感謝。 – pinaldesai 2012-04-05 06:30:24

3

我不知道你正在運行的PHP版本,但你應該使用DateTime對象,與DateInterval

它的方式更好,更易於使用。

+0

我使用Wamp服務器 - Apache版本:2.2.21 PHP版本:5.3.8 – pinaldesai 2012-03-30 12:30:30

+0

你很好走! PHP的最小版本應該是5.3.0,你的版本更高(這是爲http://php.net/datetime.createfromformat) – 2012-03-30 12:31:23

0

我認爲strtotime的週期應該是格式化的,比如周平均週日,即2012-W14-3。

for ($day=1; $day<=7; $day++) { 
    echo date('M dS, Y', strtotime(date('Y') ."-W". date('W') .'-'. $day)); 
} 
1

這可能對你有所幫助。這是返回給定年的日曆日期的功能:

<?php 

    /** 
    * Get all the dates in the given year's calendar 
    * 
    * @param NULL|integer $year 
    * @param string $format 
    * @return array 
    */ 
    function getCalendar($year = NULL, $format = 'Y-m-d') 
    { 

    // If the year is not set then use the current 
    if (!isset($year)) 
    { 
     $year = date('Y'); 
    } 

    // Get the first day of the year 
    $start = $year . '-01-01'; 

    // Iterate over an incremental day increase 
    for ($d = 0; ; ++$d) 
    { 

     // Get the date 
     $date = strtotime($start . " + $d day"); 

     // Return the current list of dates if the current date is for next year 
     if ($date >= strtotime($year + 1 . '-01-01')) 
     { 
     return $dates; 
     } 

     // Get the week number 
     $w = ceil(($d + 1)/7); 

     // Add the date to the array 
     $dates[isset($dates[$w][7]) ? ++$w : $w][date('N', $date)] = date($format, $date); 

    } 

    // Return the dates array 
    return $dates; 

    } 

    // Get the calendar for 2012 
    echo '<pre>' . print_r(getCalendar(2012, 'M dS, Y'), true) . '</pre>'; 

這將輸出:

Array 
(
    [1] => Array 
     (
      [7] => Jan 01st, 2012 
     ) 

    [2] => Array 
     (
      [1] => Jan 02nd, 2012 
      [2] => Jan 03rd, 2012 
      [3] => Jan 04th, 2012 
      [4] => Jan 05th, 2012 
      [5] => Jan 06th, 2012 
      [6] => Jan 07th, 2012 
      [7] => Jan 08th, 2012 
     ) 

    [3] => Array 
     (
      [1] => Jan 09th, 2012 
      [2] => Jan 10th, 2012 
      [3] => Jan 11th, 2012 
      [4] => Jan 12th, 2012 
      [5] => Jan 13th, 2012 
      [6] => Jan 14th, 2012 
      [7] => Jan 15th, 2012 
     ) 

    [4] => Array 
     (
      [1] => Jan 16th, 2012 
      [2] => Jan 17th, 2012 
      [3] => Jan 18th, 2012 
      [4] => Jan 19th, 2012 
      [5] => Jan 20th, 2012 
      [6] => Jan 21st, 2012 
      [7] => Jan 22nd, 2012 
     ) 

    [5] => Array 
     (
      [1] => Jan 23rd, 2012 
      [2] => Jan 24th, 2012 
      [3] => Jan 25th, 2012 
      [4] => Jan 26th, 2012 
      [5] => Jan 27th, 2012 
      [6] => Jan 28th, 2012 
      [7] => Jan 29th, 2012 
     ) 

    [6] => Array 
     (
      [1] => Jan 30th, 2012 
      [2] => Jan 31st, 2012 
      [3] => Feb 01st, 2012 
      [4] => Feb 02nd, 2012 
      [5] => Feb 03rd, 2012 
      [6] => Feb 04th, 2012 
      [7] => Feb 05th, 2012 
     ) 

    [7] => Array 
     (
      [1] => Feb 06th, 2012 
      [2] => Feb 07th, 2012 
      [3] => Feb 08th, 2012 
      [4] => Feb 09th, 2012 
      [5] => Feb 10th, 2012 
      [6] => Feb 11th, 2012 
      [7] => Feb 12th, 2012 
     ) 

    [8] => Array 
     (
      [1] => Feb 13th, 2012 
      [2] => Feb 14th, 2012 
      [3] => Feb 15th, 2012 
      [4] => Feb 16th, 2012 
      [5] => Feb 17th, 2012 
      [6] => Feb 18th, 2012 
      [7] => Feb 19th, 2012 
     ) 

    [9] => Array 
     (
      [1] => Feb 20th, 2012 
      [2] => Feb 21st, 2012 
      [3] => Feb 22nd, 2012 
      [4] => Feb 23rd, 2012 
      [5] => Feb 24th, 2012 
      [6] => Feb 25th, 2012 
      [7] => Feb 26th, 2012 
     ) 

    [10] => Array 
     (
      [1] => Feb 27th, 2012 
      [2] => Feb 28th, 2012 
      [3] => Feb 29th, 2012 
      [4] => Mar 01st, 2012 
      [5] => Mar 02nd, 2012 
      [6] => Mar 03rd, 2012 
      [7] => Mar 04th, 2012 
     ) 

    [11] => Array 
     (
      [1] => Mar 05th, 2012 
      [2] => Mar 06th, 2012 
      [3] => Mar 07th, 2012 
      [4] => Mar 08th, 2012 
      [5] => Mar 09th, 2012 
      [6] => Mar 10th, 2012 
      [7] => Mar 11th, 2012 
     ) 

    [12] => Array 
     (
      [1] => Mar 12th, 2012 
      [2] => Mar 13th, 2012 
      [3] => Mar 14th, 2012 
      [4] => Mar 15th, 2012 
      [5] => Mar 16th, 2012 
      [6] => Mar 17th, 2012 
      [7] => Mar 18th, 2012 
     ) 

    [13] => Array 
     (
      [1] => Mar 19th, 2012 
      [2] => Mar 20th, 2012 
      [3] => Mar 21st, 2012 
      [4] => Mar 22nd, 2012 
      [5] => Mar 23rd, 2012 
      [6] => Mar 24th, 2012 
      [7] => Mar 25th, 2012 
     ) 

    [14] => Array 
     (
      [1] => Mar 26th, 2012 
      [2] => Mar 27th, 2012 
      [3] => Mar 28th, 2012 
      [4] => Mar 29th, 2012 
      [5] => Mar 30th, 2012 
      [6] => Mar 31st, 2012 
      [7] => Apr 01st, 2012 
     ) 

    [15] => Array 
     (
      [1] => Apr 02nd, 2012 
      [2] => Apr 03rd, 2012 
      [3] => Apr 04th, 2012 
      [4] => Apr 05th, 2012 
      [5] => Apr 06th, 2012 
      [6] => Apr 07th, 2012 
      [7] => Apr 08th, 2012 
     ) 

    [16] => Array 
     (
      [1] => Apr 09th, 2012 
      [2] => Apr 10th, 2012 
      [3] => Apr 11th, 2012 
      [4] => Apr 12th, 2012 
      [5] => Apr 13th, 2012 
      [6] => Apr 14th, 2012 
      [7] => Apr 15th, 2012 
     ) 

    [17] => Array 
     (
      [1] => Apr 16th, 2012 
      [2] => Apr 17th, 2012 
      [3] => Apr 18th, 2012 
      [4] => Apr 19th, 2012 
      [5] => Apr 20th, 2012 
      [6] => Apr 21st, 2012 
      [7] => Apr 22nd, 2012 
     ) 

    [18] => Array 
     (
      [1] => Apr 23rd, 2012 
      [2] => Apr 24th, 2012 
      [3] => Apr 25th, 2012 
      [4] => Apr 26th, 2012 
      [5] => Apr 27th, 2012 
      [6] => Apr 28th, 2012 
      [7] => Apr 29th, 2012 
     ) 

    [19] => Array 
     (
      [1] => Apr 30th, 2012 
      [2] => May 01st, 2012 
      [3] => May 02nd, 2012 
      [4] => May 03rd, 2012 
      [5] => May 04th, 2012 
      [6] => May 05th, 2012 
      [7] => May 06th, 2012 
     ) 

    [20] => Array 
     (
      [1] => May 07th, 2012 
      [2] => May 08th, 2012 
      [3] => May 09th, 2012 
      [4] => May 10th, 2012 
      [5] => May 11th, 2012 
      [6] => May 12th, 2012 
      [7] => May 13th, 2012 
     ) 

    [21] => Array 
     (
      [1] => May 14th, 2012 
      [2] => May 15th, 2012 
      [3] => May 16th, 2012 
      [4] => May 17th, 2012 
      [5] => May 18th, 2012 
      [6] => May 19th, 2012 
      [7] => May 20th, 2012 
     ) 

    [22] => Array 
     (
      [1] => May 21st, 2012 
      [2] => May 22nd, 2012 
      [3] => May 23rd, 2012 
      [4] => May 24th, 2012 
      [5] => May 25th, 2012 
      [6] => May 26th, 2012 
      [7] => May 27th, 2012 
     ) 

    [23] => Array 
     (
      [1] => May 28th, 2012 
      [2] => May 29th, 2012 
      [3] => May 30th, 2012 
      [4] => May 31st, 2012 
      [5] => Jun 01st, 2012 
      [6] => Jun 02nd, 2012 
      [7] => Jun 03rd, 2012 
     ) 

    [24] => Array 
     (
      [1] => Jun 04th, 2012 
      [2] => Jun 05th, 2012 
      [3] => Jun 06th, 2012 
      [4] => Jun 07th, 2012 
      [5] => Jun 08th, 2012 
      [6] => Jun 09th, 2012 
      [7] => Jun 10th, 2012 
     ) 

    [25] => Array 
     (
      [1] => Jun 11th, 2012 
      [2] => Jun 12th, 2012 
      [3] => Jun 13th, 2012 
      [4] => Jun 14th, 2012 
      [5] => Jun 15th, 2012 
      [6] => Jun 16th, 2012 
      [7] => Jun 17th, 2012 
     ) 

    [26] => Array 
     (
      [1] => Jun 18th, 2012 
      [2] => Jun 19th, 2012 
      [3] => Jun 20th, 2012 
      [4] => Jun 21st, 2012 
      [5] => Jun 22nd, 2012 
      [6] => Jun 23rd, 2012 
      [7] => Jun 24th, 2012 
     ) 

    [27] => Array 
     (
      [1] => Jun 25th, 2012 
      [2] => Jun 26th, 2012 
      [3] => Jun 27th, 2012 
      [4] => Jun 28th, 2012 
      [5] => Jun 29th, 2012 
      [6] => Jun 30th, 2012 
      [7] => Jul 01st, 2012 
     ) 

    [28] => Array 
     (
      [1] => Jul 02nd, 2012 
      [2] => Jul 03rd, 2012 
      [3] => Jul 04th, 2012 
      [4] => Jul 05th, 2012 
      [5] => Jul 06th, 2012 
      [6] => Jul 07th, 2012 
      [7] => Jul 08th, 2012 
     ) 

    [29] => Array 
     (
      [1] => Jul 09th, 2012 
      [2] => Jul 10th, 2012 
      [3] => Jul 11th, 2012 
      [4] => Jul 12th, 2012 
      [5] => Jul 13th, 2012 
      [6] => Jul 14th, 2012 
      [7] => Jul 15th, 2012 
     ) 

    [30] => Array 
     (
      [1] => Jul 16th, 2012 
      [2] => Jul 17th, 2012 
      [3] => Jul 18th, 2012 
      [4] => Jul 19th, 2012 
      [5] => Jul 20th, 2012 
      [6] => Jul 21st, 2012 
      [7] => Jul 22nd, 2012 
     ) 

    [31] => Array 
     (
      [1] => Jul 23rd, 2012 
      [2] => Jul 24th, 2012 
      [3] => Jul 25th, 2012 
      [4] => Jul 26th, 2012 
      [5] => Jul 27th, 2012 
      [6] => Jul 28th, 2012 
      [7] => Jul 29th, 2012 
     ) 

    [32] => Array 
     (
      [1] => Jul 30th, 2012 
      [2] => Jul 31st, 2012 
      [3] => Aug 01st, 2012 
      [4] => Aug 02nd, 2012 
      [5] => Aug 03rd, 2012 
      [6] => Aug 04th, 2012 
      [7] => Aug 05th, 2012 
     ) 

    [33] => Array 
     (
      [1] => Aug 06th, 2012 
      [2] => Aug 07th, 2012 
      [3] => Aug 08th, 2012 
      [4] => Aug 09th, 2012 
      [5] => Aug 10th, 2012 
      [6] => Aug 11th, 2012 
      [7] => Aug 12th, 2012 
     ) 

    [34] => Array 
     (
      [1] => Aug 13th, 2012 
      [2] => Aug 14th, 2012 
      [3] => Aug 15th, 2012 
      [4] => Aug 16th, 2012 
      [5] => Aug 17th, 2012 
      [6] => Aug 18th, 2012 
      [7] => Aug 19th, 2012 
     ) 

    [35] => Array 
     (
      [1] => Aug 20th, 2012 
      [2] => Aug 21st, 2012 
      [3] => Aug 22nd, 2012 
      [4] => Aug 23rd, 2012 
      [5] => Aug 24th, 2012 
      [6] => Aug 25th, 2012 
      [7] => Aug 26th, 2012 
     ) 

    [36] => Array 
     (
      [1] => Aug 27th, 2012 
      [2] => Aug 28th, 2012 
      [3] => Aug 29th, 2012 
      [4] => Aug 30th, 2012 
      [5] => Aug 31st, 2012 
      [6] => Sep 01st, 2012 
      [7] => Sep 02nd, 2012 
     ) 

    [37] => Array 
     (
      [1] => Sep 03rd, 2012 
      [2] => Sep 04th, 2012 
      [3] => Sep 05th, 2012 
      [4] => Sep 06th, 2012 
      [5] => Sep 07th, 2012 
      [6] => Sep 08th, 2012 
      [7] => Sep 09th, 2012 
     ) 

    [38] => Array 
     (
      [1] => Sep 10th, 2012 
      [2] => Sep 11th, 2012 
      [3] => Sep 12th, 2012 
      [4] => Sep 13th, 2012 
      [5] => Sep 14th, 2012 
      [6] => Sep 15th, 2012 
      [7] => Sep 16th, 2012 
     ) 

    [39] => Array 
     (
      [1] => Sep 17th, 2012 
      [2] => Sep 18th, 2012 
      [3] => Sep 19th, 2012 
      [4] => Sep 20th, 2012 
      [5] => Sep 21st, 2012 
      [6] => Sep 22nd, 2012 
      [7] => Sep 23rd, 2012 
     ) 

    [40] => Array 
     (
      [1] => Sep 24th, 2012 
      [2] => Sep 25th, 2012 
      [3] => Sep 26th, 2012 
      [4] => Sep 27th, 2012 
      [5] => Sep 28th, 2012 
      [6] => Sep 29th, 2012 
      [7] => Sep 30th, 2012 
     ) 

    [41] => Array 
     (
      [1] => Oct 01st, 2012 
      [2] => Oct 02nd, 2012 
      [3] => Oct 03rd, 2012 
      [4] => Oct 04th, 2012 
      [5] => Oct 05th, 2012 
      [6] => Oct 06th, 2012 
      [7] => Oct 07th, 2012 
     ) 

    [42] => Array 
     (
      [1] => Oct 08th, 2012 
      [2] => Oct 09th, 2012 
      [3] => Oct 10th, 2012 
      [4] => Oct 11th, 2012 
      [5] => Oct 12th, 2012 
      [6] => Oct 13th, 2012 
      [7] => Oct 14th, 2012 
     ) 

    [43] => Array 
     (
      [1] => Oct 15th, 2012 
      [2] => Oct 16th, 2012 
      [3] => Oct 17th, 2012 
      [4] => Oct 18th, 2012 
      [5] => Oct 19th, 2012 
      [6] => Oct 20th, 2012 
      [7] => Oct 21st, 2012 
     ) 

    [44] => Array 
     (
      [1] => Oct 22nd, 2012 
      [2] => Oct 23rd, 2012 
      [3] => Oct 24th, 2012 
      [4] => Oct 25th, 2012 
      [5] => Oct 26th, 2012 
      [6] => Oct 27th, 2012 
      [7] => Oct 28th, 2012 
     ) 

    [45] => Array 
     (
      [1] => Oct 29th, 2012 
      [2] => Oct 30th, 2012 
      [3] => Oct 31st, 2012 
      [4] => Nov 01st, 2012 
      [5] => Nov 02nd, 2012 
      [6] => Nov 03rd, 2012 
      [7] => Nov 04th, 2012 
     ) 

    [46] => Array 
     (
      [1] => Nov 05th, 2012 
      [2] => Nov 06th, 2012 
      [3] => Nov 07th, 2012 
      [4] => Nov 08th, 2012 
      [5] => Nov 09th, 2012 
      [6] => Nov 10th, 2012 
      [7] => Nov 11th, 2012 
     ) 

    [47] => Array 
     (
      [1] => Nov 12th, 2012 
      [2] => Nov 13th, 2012 
      [3] => Nov 14th, 2012 
      [4] => Nov 15th, 2012 
      [5] => Nov 16th, 2012 
      [6] => Nov 17th, 2012 
      [7] => Nov 18th, 2012 
     ) 

    [48] => Array 
     (
      [1] => Nov 19th, 2012 
      [2] => Nov 20th, 2012 
      [3] => Nov 21st, 2012 
      [4] => Nov 22nd, 2012 
      [5] => Nov 23rd, 2012 
      [6] => Nov 24th, 2012 
      [7] => Nov 25th, 2012 
     ) 

    [49] => Array 
     (
      [1] => Nov 26th, 2012 
      [2] => Nov 27th, 2012 
      [3] => Nov 28th, 2012 
      [4] => Nov 29th, 2012 
      [5] => Nov 30th, 2012 
      [6] => Dec 01st, 2012 
      [7] => Dec 02nd, 2012 
     ) 

    [50] => Array 
     (
      [1] => Dec 03rd, 2012 
      [2] => Dec 04th, 2012 
      [3] => Dec 05th, 2012 
      [4] => Dec 06th, 2012 
      [5] => Dec 07th, 2012 
      [6] => Dec 08th, 2012 
      [7] => Dec 09th, 2012 
     ) 

    [51] => Array 
     (
      [1] => Dec 10th, 2012 
      [2] => Dec 11th, 2012 
      [3] => Dec 12th, 2012 
      [4] => Dec 13th, 2012 
      [5] => Dec 14th, 2012 
      [6] => Dec 15th, 2012 
      [7] => Dec 16th, 2012 
     ) 

    [52] => Array 
     (
      [1] => Dec 17th, 2012 
      [2] => Dec 18th, 2012 
      [3] => Dec 19th, 2012 
      [4] => Dec 20th, 2012 
      [5] => Dec 21st, 2012 
      [6] => Dec 22nd, 2012 
      [7] => Dec 23rd, 2012 
     ) 

    [53] => Array 
     (
      [1] => Dec 24th, 2012 
      [2] => Dec 25th, 2012 
      [3] => Dec 26th, 2012 
      [4] => Dec 27th, 2012 
      [5] => Dec 28th, 2012 
      [6] => Dec 29th, 2012 
      [7] => Dec 30th, 2012 
     ) 

    [54] => Array 
     (
      [1] => Dec 31st, 2012 
     ) 

) 

正如你可以看到,它的形式$array[$week_number][$day_number](其中1 $day_number是星期一和7日是星期日),$week_number將高達53或54(取決於年份)。

然後,您可以用獲取特定的星期日期:從以前的或未來幾年在開始和結束

$calendar = getCalendar(); 

$calendar[9]; 

// Or if you have PHP 5.4.0 

getCalendar()[9]; 

我故意不包括天。

1

下面的代碼

$ts = mktime(0, 0, 0, 4, 5, 2012); 

$offset = date('N', $ts); 

for ($i = 1 - $offset, $j = 0; $j++ < 7; $i++) { 
    echo date('r', $ts + $i * 3600 * 24) . "\n"; 
} 

一週的產出日期的指定日屬於:

Apr 02nd, 2012 
Apr 03rd, 2012 
Apr 04th, 2012 
Apr 05th, 2012 
Apr 06th, 2012 
Apr 07th, 2012 
Apr 08th, 2012 

我希望這是你所追求的。

至於用的strtotime檢查問題,如果返回的日期是真的你是後做

echo date('r', strtotime('03-09-2012')); 

它是更安全的傳遞日期的strtotime如YYYY-MM-DD的日期。

0

這應該適合你。第一行中的「今日」可以更改爲您想要測試的日期。

<?php 
$today = strtotime('today'); 
$startWeek = strtotime(date('o-\\WW', $today)); 
$date = mktime(0, 0, 0, date('m', $startWeek), date('d', $startWeek), date('Y', $startWeek)); 
$week = (int)date('W', $date); 
$year = date('Y', $date); 

for($day=0; $day < 7; $day++) 
{ 
    echo date('M dS, Y', $today + ($day * 24 * 60 * 60)), "\n"; 
} 
?> 

希望這會有所幫助。

1

使用DateTime類(PHP 5> = 5.2.0):

$date = date_create(); 

    for($d=1; $d<=7; $d++){ 

     echo $date->format('M dS, Y')."<br />";   
     $date->modify('+1 day'); 

    } 
1

嘗試此。它適用於我:

<?php 
if(isset($_GET['diffweek'])){ 
    $diffweek=$_GET['diffweek']; 
}else{ 
    $diffweek=0;} 

$date = mktime(0, 0, 0, date('m'), date('d'), date('Y')); 
$week = date('W', $date+$diffweek*604800); 
$year = date('Y'); 

for($day=1; $day<=7; $day++) 
    { 
    echo date('M dS, Y', strtotime($year."W".$week.$day)); 
    echo '<hr>'; 
    } 

echo '<a href="?diffweek='.($diffweek-4).'">4 weeks back</a><br>'; 
echo '<a href="?diffweek='.($diffweek-1).'">prew. week</a><br>'; 
echo '<a href="?diffweek=0">this week</a><br>'; 
echo '<a href="?diffweek='.($diffweek+1).'">next week</a><br>'; 
echo '<a href="?diffweek='.($diffweek+4).'">4 week later</a><br>'; 
?>