【问题标题】:How do I get next date by day of week and time如何按星期几和时间获取下一个日期
【发布时间】:2019-06-09 09:37:20
【问题描述】:

我想按星期几和时间获取下一节课的日期。例如,所有课程都在周二和周六晚上 10:45 举行 所以我想显示从当前日期/时间开始的下一堂课的日期。

没有时间我就能得到。但是,如果今天是星期二下午 4 点,上课时间是星期二晚上 10 点 45 分,那么它应该显示今天的日期,而不是星期六,因为今天之后的下一堂课是星期六。

我尝试了一段时间的测试代码,但它不起作用。

$nextTuesday = strtotime('next tuesday 10:59PM');
echo date('Y-m-d', $nextTuesday);

如果我给时间它是行不通的。

如果日期相同且时间尚未过去,则应显示当前日期。

【问题讨论】:

  • 错误信息是什么?
  • 恐怕你一个语句做不到。
  • 不,它在两行中,但在这里粘贴在一行中
  • 它没有显示错误。它显示我下周二 2019-01-22。但它应该显示 2019-01-15 星期二,因为它还没有 10:45
  • 你已经创建了你自己的逻辑。

标签: php date time weekday


【解决方案1】:

好的,我用我的逻辑解决了它。

    date_default_timezone_set("ET");
    $currentday=date('Y-m-d H:i:s');
    $currentdayW=date("l", strtotime($currentday));



    if (($currentdayW=="Tuesday") ||($currentdayW=="Friday"))
    {
    $chktime = date('Y-m-d 21:00:00');
    if ($chktime > $currentday) { $drawdate =  $chktime;}

    }

    if ((($currentdayW=="Tuesday") && ($chktime < $currentday)) || 
     ($currentdayW=="Wednesday") || ($currentdayW=="Thursday"))
    {
    $drawdate = strtotime('next friday');
    $drawdate=date('Y-m-d 21:00:00', $drawdate);
    }
    if ((($currentdayW=="Friday") && ($chktime < $currentday)) || 
    ($currentdayW=="Saturday") || ($currentdayW=="Sunday") || 
    ($currentdayW=="Monday"))
    {
     $drawdate = strtotime('next tuesday');
     $drawdate=date('Y-m-d 21:00:00', $drawdate);

     }

     echo $drawdate;

【讨论】:

    猜你喜欢
    • 2015-01-01
    • 1970-01-01
    • 2017-12-16
    • 1970-01-01
    • 2015-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-23
    相关资源
    最近更新 更多