简体   繁体   English

比较星期几和小时到现在

[英]compare day of week and hour to now

I am looking to figure out if current time falls in between two day and hour schedules, to figure out if we need to display some content (go live) or not,我想弄清楚当前时间是否介于两天和小时的时间表之间,以确定我们是否需要显示一些内容(上线),

I am using date_i18n() function, because I am in wordpress, and need the date in the configured timezone.我正在使用 date_i18n() function,因为我在 wordpress,并且需要配置时区中的日期。

here is what I have so far:这是我到目前为止所拥有的:

// live = Tuesday at 5:00pm thorugh Sunday at 4:59pm
// not live = Sunday at 5:00pm -> Tuesday at 4:59pm

$now = date_i18n('w-G'); // Date format: Week-Day: 0-6 - Hour: 1-23
echo "Now = ".$now."<br/>";

$live_start = date_i18n('w-G', '2-17'); // = date_i18n('w-G', strtotime('2-17'));
echo "Live Start: ".$live_start."<br/>";

$live_end = date_i18n('w-G', '0-16'); //date_i18n('w-G', strtotime('0-16'));
echo "Live End: ".$live_end."<br/>";

echo "<b>".$now.">=".$live_start." && ".$now."<=".$live_end."</b><br/>";

if(($now >= $live_start) && ($now <= $live_end)){
  echo "Live";
}else{
  echo "Not Live";
}

This is what I get:这就是我得到的:

Now = 1-13  
Live Start: 1-13  
Live End: 1-13  
1-13>=1-13 && 1-13<=1-13   
Live

I am not sure what it is happening, Current time is correct, Monday 13th hour.我不确定它发生了什么,当前时间是正确的,星期一 13 小时。 I have tried adding the strtotime() and "" but no change.我尝试添加 strtotime() 和 "" 但没有改变。

any ideas how to fix it, or maybe the entire code is bad and there is a better way?任何想法如何解决它,或者整个代码可能很糟糕并且有更好的方法? thanks.谢谢。

I don't think the data type that you're getting from date_i18ndate_i18n('w-G') can be compared like that.我不认为您从 date_i18ndate_i18n('w-G') 获得的数据类型可以这样比较。 Try breaking it into the individual parts, so that you have values that can be compared.尝试将其分解为各个部分,以便您拥有可以比较的值。

$nowDay = date_i18n('w');
$nowHour = date_i18n('G'); 

$startDay = 2;
$startHour = 17;

$endDay = 0;
$endHour = 16;


switch ($nowDay) {
    case 0:
        if($nowHour >= $endHour){
          echo "Not Live";
        }else{
          echo "Live";
        }
    case 1:
        echo "Not Live";
    case 2:
        if($nowHour >= $startHour){
          echo "Live";
        }else{
          echo "Not Live";
        }
    case 3:
        echo "Live";
    case 4:
        echo "Live";
    case 5:
        echo "Live";
    case 6:
        echo "Live";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM