[英]PHP generate timestamp for each day of the week
我正在尝试为一周中的每一天生成一个时间戳。 我的一周从星期一开始,到星期日结束。
到目前为止,我的代码如下。 问题在于,今天(星期六)生成的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');
}
$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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.