【问题标题】:Show date of next thursday [duplicate]显示下周四的日期[重复]
【发布时间】:2017-11-23 11:35:09
【问题描述】:

我使用下面的代码来显示每个下一个星期四的日期。

现在我遇到了以下问题:今天是星期四,但我不想在当天结束之前显示下一周的日期。这怎么可能?

每个星期四晚上 7 点到 10 点我们都有值班。因此,例如,我需要显示今天的日期。晚上 10 点之后,我可以显示下一个星期四的日期(例如 30.11。)

当前代码:

 $timestmp = strtotime('next thursday');
 setlocale(LC_TIME, "de_DE");
 $next = strftime('%A, %d.%m.%Y', $timestmp); 

【问题讨论】:

    标签: php date timestamp strtotime


    【解决方案1】:

    你可以有一个简单的if condition 来检查今天是否是星期四,打印今天的日期。否则,下周四打印

    setlocale(LC_TIME, "de_DE");
    
    if (date('l') == 'Thursday') {
        $thu = strftime('%A, %d.%m.%Y');
    } else {
        $timestmp = strtotime('next thursday');
        $thu = strftime('%A, %d.%m.%Y', $timestmp);
    }
    
    echo $thu;
    

    【讨论】:

      【解决方案2】:

      如果(日期('l')=='星期四'){ echo '今天是星期四!哇! } 别的 { $timestmp = strtotime('下一个星期四'); setlocale(LC_TIME, "de_DE"); $next = strftime('%A, %d.%m.%Y', $timestmp); }

      【讨论】:

        【解决方案3】:

        所以你想在这个星期四的今天演出吗? 然后传给strtotime,昨天的时间戳。就这样

         $timestmp = strtotime('next thursday', strtotime('-1 day'));
        

        那么它会从昨天开始寻找下一个星期四,也就是今天。

        【讨论】:

        • 嗯,这不是我想要的。我从当地的志愿消防部门管理网站。每个星期四我们从晚上 7 点到晚上 10 点都有值班。因此,例如,我需要显示今天的日期。晚上 10 点之后,我可以显示下一个星期四的日期(例如 30.11。)
        【解决方案4】:

        如果是星期日、星期一、星期二或星期三,以下代码会抓取下一个星期四。如果是星期五或星期六,它会抓住最后一个星期四。如果今天是星期四,它将抓住当天。

        $now = new DateTime();
        if ($now->format('w') < 4) {
            $now->modify('next thursday')
        } elseif ($now->format('w') > 4) {
            $now->modify('last thursday')
        }
        
        echo $now->format('Y-m-d');
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-01-08
          • 1970-01-01
          • 2018-09-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多