2016-03-05 63 views
0

我試圖爲每週的每一天生成時間戳。我的周開始星期一和結束太陽。PHP爲每週的每一天生成時間戳

我的代碼至今爲止如下。問題是,今天(星期六),生成的Sun時間戳是「上週日」,但應該是「下個星期日」。

我不知道什麼最好的方法是不使用if/else語句的負載。

$today = time(); 
    //$today = strtotime('next monday'); 

    // If today is monday, generate timestamps for each day of the week starting today 
    if(date('D', $today) == 'Mon') { 
     $mon = $today; 
     $tue = strtotime('next tuesday'); 
     $wed = strtotime('next wednesday'); 
     $thu = strtotime('next thursday'); 
     $fri = strtotime('next friday'); 
     $sat = strtotime('next saturday'); 
     $sun = strtotime('next sunday'); 
    } 
    // Today is not monday, so generate timestamps for each day of the week, starting last monday 
    else { 
     $mon = strtotime('last monday'); 
     $tue = (date('D', $today) == 'Tue') ? $today : strtotime('last tuesday'); 
     $wed = (date('D', $today) == 'Wed') ? $today : strtotime('last wednesday'); 
     $thu = (date('D', $today) == 'Thu') ? $today : strtotime('last thursday'); 
     $fri = (date('D', $today) == 'Fri') ? $today : strtotime('last friday'); 
     $sat = (date('D', $today) == 'Sat') ? $today : strtotime('last saturday'); 
     $sun = (date('D', $today) == 'Sun') ? $today : strtotime('last sunday'); 
    } 
+0

'$週二= $今天+(60 * 60 *(1 * 24)); $ wed = $ today +(60 * 60 *(2 * 24));' –

回答

1
$now = time(); 

if(date('D', $now) == 'Mon') { 
    $mon = mktime(0,0,0); 
} else { 
    $mon = strtotime('last monday'); 
} 
$tue = strtotime('+1 day', $mon); 
$wed = strtotime('+1 day', $tue); 
$thu = strtotime('+1 day', $wed); 
$fri = strtotime('+1 day', $thu); 
$sat = strtotime('+1 day', $fri); 
$sun = strtotime('+1 day', $sat); 
相關問題