【问题标题】:PHP generate timestamp for each day of the weekPHP为一周中的每一天生成时间戳
【发布时间】: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


【解决方案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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-06
    • 2019-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-20
    • 2019-08-12
    • 2021-06-11
    相关资源
    最近更新 更多