首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP strtotime()并根据两个给定时间比较当前时间

PHP strtotime()并根据两个给定时间比较当前时间
EN

Stack Overflow用户
提问于 2013-05-09 12:39:23
回答 2查看 1.1K关注 0票数 1

我在基于两个预先指定的时间比较当前时间时遇到了问题。让我告诉你我想要什么:如果当前时间在晚上10点到早上8点之间,我想显示一条消息我的代码是

代码语言:javascript
复制
$current_time = strtotime('now');
if ( $current_time > strtotime('10:00pm') && $current_time < strtotime('8:00am') ) { 
   //Display message
}

但是它不起作用,因为在12:00 in之后,strtotime(‘10:00 in’)正在计算今天的10 in值,并且在给定的条件下无法传递。所以我需要你的建议。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-05-09 14:37:23

date("G", strtotime("now"))将显示24小时格式,不带前导零。

你想要的代码是:

代码语言:javascript
复制
$current_hour = date("G", strtotime("now"));
if ($current_hour >= 22 || $current_hour < 8) {
    //Display message
}
票数 1
EN

Stack Overflow用户

发布于 2013-05-09 14:08:42

尝尝这个,

代码语言:javascript
复制
     $current_hour = date("G", strtotime("now"));
     if ($current_hour > 23 || $current_hour < 8) {
     if ( $current_time > strtotime('10:00pm -1 Day') && $current_time < strtotime('8:00am') ) { //after 
     //Display message
    }
     }else{
     if ( $current_time > strtotime('10:00pm') && $current_time < strtotime('8:00am +1 Day') ) { 
    //Display message
      }
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16454482

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档