【问题标题】:Determining if day has passed PHP确定一天是否已过 PHP
【发布时间】:2020-09-02 17:45:59
【问题描述】:

试图弄清楚以下内容:

我的俱乐部每周运营 3 天,周二、周四和周六。

我想打印本周的下一个俱乐部日期,因此如果周二是下一次会议,则打印“周二”,或者如果今天是周二,则打印“今天”,周四和周六也是如此。

我有以下代码:

<?php
  $current = date('l', strtotime('now'));
  $tuesday = date('l', strtotime('tuesday this week'));
  $thursday = date('l', strtotime('thursday this week'));
  $saturday = date('l', strtotime('saturday this week'));

  if ($tuesday < $current) {
    echo "Tuesday in the future";
  } else if ($thursday < $current) {
    echo "Thursday in the future";
  } else if ($saturday < $current) {
    echo "Saturday in the future";
  }
 ?>

这就是我做不到的地方,无法弄清楚....因为它总是只打印“未来的星期二”。

非常欢迎任何帮助、建议或解释!

谢谢 E

【问题讨论】:

  • 请记住,这种不指定小时的方法将返回Today,即使在一天的训练中已经是23:59。

标签: php


【解决方案1】:

您正在将字符串与date('l') 进行比较。把date('l')换成date('Ymd'),或者直接离开strtotime,就可以比较数字了。如果是星期天,你可能需要一个“下周星期二”。

<?php

$current = strtotime('now');
$tuesday = strtotime('tuesday this week');
$thursday = strtotime('thursday this week');
$saturday = strtotime('saturday this week');
$nextTuesday = strtotime('tuesday next week');

if ($tuesday > $current) {
    echo "Tuesday in the future";
} else if ($thursday > $current) {
    echo "Thursday in the future";
} else if ($saturday > $current) {
    echo "Saturday in the future";
} else if($nextTuesday > $current) {
    echo "Tuesday in the future";
}

【讨论】:

  • strtotime 本身就可以解决问题,因为它返回一个 unix 时间戳
  • 好电话@RisingSun
  • 非常感谢您的帮助!它真的帮了很多忙!虽然我仍然有1个问题,如果今天是今天,我将如何打印“今天”,例如,今天是星期四,但代码的输出是“星期六”,理想情况下我希望它是“星期四”因为我们的俱乐部之夜是今晚! :) TIA
  • 您可以查看俱乐部开放的星期几的日期,例如if (in_array(date('l'), ['Tuesday','Thursday','Saturday'])) { echo "Today!";}
  • 谢谢!!让我运行一个场景,如果星期二已经过去,我如何向 div 添加一个类,例如 if != tuesday echo "d-none" 那种事情......?其他日子也一样! :) TIA
【解决方案2】:

因为来自 PHP 文档:

l(小写 'L') : 星期几的完整文本表示

你可以使用:

U :自 Unix 纪元以来的秒数(格林威治标准时间 1970 年 1 月 1 日 00:00:00)

但是在你的代码中你有$tuesday &lt; $current,这是没有意义的,因为如果$tuesday 比过去小于 $current,你应该使用&gt;:

<?php
  $current = date('U', strtotime('now'));
  $tuesday = date('U', strtotime('tuesday this week'));
  $thursday = date('U', strtotime('thursday this week'));
  $saturday = date('U', strtotime('saturday this week'));
  if ($tuesday > $current) {
    echo "Tuesday in the future";
  } else if ($thursday > $current) {
    echo "Thursday in the future";
  } else if ($saturday > $current) {
    echo "Saturday in the future";
  }
 ?>

【讨论】:

    【解决方案3】:

    您的代码的问题是您比较了string 类型的变量,这将无法按您的意愿工作。

    我使用了另一种方法,将数组与训练列表进行比较,您可以在未来对其进行修改。希望能给大家一些启发。

    我使用了date('N'),它给出了1 (for Monday) through 7 (for Sunday): https://www.php.net/manual/en/datetime.format.php

    $trainings = [
        '2' => 'Tuesday',
        '4' => 'Thursday',
        '6' => 'Saturday'
    ];
    
    $today = date('l');
    $todayN = date('N');
    
    if (in_array($today, $trainings)) {
        echo 'Today';
    } else {
        $nextTraining = array_values($trainings)[0];
        foreach ($trainings as $n => $day) {
            if ($todayN < $n) {
                $nextTraining = $day;
                break;
            }
        }
        echo $nextTraining;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-11
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-28
      相关资源
      最近更新 更多