【发布时间】:2016-03-05 19:21:49
【问题描述】:
我正在尝试为一周中的每一天生成一个时间戳。我的一周从周一开始,到周日结束。
到目前为止,我的代码如下。问题是今天(星期六),为 Sun 生成的时间戳是“last sunday”,但应该是“next sunday”。
如果不使用大量 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');
}
【问题讨论】:
-
$tue = $today + (60 * 60 * (1 * 24)); $wed = $today + (60 * 60 * (2 * 24));
标签: php