2011-03-27 56 views
7

我有當天的日期時間。我需要得到兩個unix開始和結束本週的時間戳。我如何使用dateperiod或dateinterval類?獲取本週的時間戳

回答

12
$now = time(); 
$beginning_of_week = strtotime('last Monday', $now); // Gives you the time at the BEGINNING of the week 
$end_of_week = strtotime('next Sunday', $now) + 86400; // Gives you the time at the END of the last day of the week 
+3

但什麼做當今天是星期一?我得到了14天的一週。 – Molfar 2011-03-29 17:03:06

+4

然後檢查今天是否是星期一,以及是否將'上週一'替換爲'今天'。要查看它是否是當前星期一,請執行'if(date('w',$ now())== 1)' – jackbot 2011-03-29 17:18:33

+0

似乎也應該附加「UTC」以獲得UTC0時間戳 – 4esn0k 2011-11-03 06:45:35

2
public static function getDaysInWeek($timestamp) 
{  
    $monday = idate('w', $timestamp) == 1 ? $timestamp : strtotime("last Monday", $timestamp); 

    $days = array(); 
    for ($i = 0; $i < 7; ++$i) 
    { 
     $days[$i] = strtotime('+' . $i . ' days', $monday); 
    } 
    return $days; 
} 
5
if (date('w', time()) == 1) 
    $beginning_of_week = strtotime('Today',time()); 
else 
    $beginning_of_week = strtotime('last Monday',time()); 

if (date('w', time()) == 7) 
    $end_of_week = strtotime('Today', time()) + 86400; 
else 
    $end_of_week = strtotime('next Sunday', time()) + 86400;