Php 检查两次之间的时间(进入夜间-第二天?)

Php 检查两次之间的时间(进入夜间-第二天?),php,datetime,time,Php,Datetime,Time,我必须在特定时间之间进行检查,以运行特定的代码,它必须从晚上8点运行到凌晨3点,每当我执行时间/检查时,它总是以建议的常规方法失败,因为我相信我们会进入第二天早上的时间,对吗?如果是这样,我该怎么做?这似乎不可能 i、 e $now=新日期时间(“now”); $begintime=新日期时间('18:00'); $endtime=新日期时间('03:00'); 如果($now>=$begintime&&$now尝试!($now>$endtime&&$now$endtime&$now$endti

我必须在特定时间之间进行检查,以运行特定的代码,它必须从晚上8点运行到凌晨3点,每当我执行时间/检查时,它总是以建议的常规方法失败,因为我相信我们会进入第二天早上的时间,对吗?如果是这样,我该怎么做?这似乎不可能

i、 e

$now=新日期时间(“now”);
$begintime=新日期时间('18:00');
$endtime=新日期时间('03:00');
如果($now>=$begintime&&$now尝试
!($now>$endtime&&$now<$begintime)
3:00am
8:00pm
之间的同一天错误,或者您可以错误地删除
,但是您必须从
3:00am
8:00pm
进行检查,而不是从
8:00am

$now = new Datetime("now");
$begintime = new DateTime('20:00');
$endtime = new DateTime('03:00');
if(!( $now > $endtime && $now < $begintime) ){
    //This will work if Time between 8PM to 3AM
}else{
    //This will work if Time between 3AM to 8PM
}
$now=新日期时间(“now”);
$begintime=新日期时间('20:00');
$endtime=新日期时间('03:00');
如果(!($now>$endtime&$now<$begintime)){
//如果时间介于晚上8点到凌晨3点之间,此功能将起作用
}否则{
//如果时间介于凌晨3点到晚上8点之间,此功能将正常工作
}
试试
!($now>$endtime&$now<$begintime)
这是在
3:00am
8:00pm
之间的同一天的错误时间,或者你可以错误地删除
,但是你必须从
3:00am
8:00pm
,而不是从
8:00pm
3:00am

$now = new Datetime("now");
$begintime = new DateTime('20:00');
$endtime = new DateTime('03:00');
if(!( $now > $endtime && $now < $begintime) ){
    //This will work if Time between 8PM to 3AM
}else{
    //This will work if Time between 3AM to 8PM
}
$now=新日期时间(“now”);
$begintime=新日期时间('20:00');
$endtime=新日期时间('03:00');
如果(!($now>$endtime&$now<$begintime)){
//如果时间介于晚上8点到凌晨3点之间,此功能将起作用
}否则{
//如果时间介于凌晨3点到晚上8点之间,此功能将正常工作
}
试试这个

$today = strtotime("today");
$yesterday = strtotime("Yesterday");
if(date("H"); >= 0 && date("H") <= 3)
{//Means we are dealing with this after midnight but still before 3AM so take yesterday as start time.
$day = date("d", $yesterday);
$month = date("m", $yesterday);
$year = date("Y", $yesterday);
$begintime = mktime(18,0,0,$month, $day, $year);
}
else
{//We are dealing with it after 3 AM so we take today's 6 PM as start time.
$day = date("d", $today);
$month = date("m", $today);
$year = date("Y", $today);
$begintime = mktime(18, 0, 0, $month, $day, $year);
//Could have taken day+1 but doesn't works at the end of the month or end of the year.
//So just add number of seconds for 9 hours
$endtime = $begintime + 32400;//

$now = time();
if($now >= $begintime && $now <= $endtime){
    die('x');
}
else {
//Run your code that you wanted to run outside this timeframe
}
$today=strotime(“今天”);
$昨天=标准时间(“昨天”);
如果(date(“H”);>=0&&date(“H”)=$begintime&&$now试试这个

$today = strtotime("today");
$yesterday = strtotime("Yesterday");
if(date("H"); >= 0 && date("H") <= 3)
{//Means we are dealing with this after midnight but still before 3AM so take yesterday as start time.
$day = date("d", $yesterday);
$month = date("m", $yesterday);
$year = date("Y", $yesterday);
$begintime = mktime(18,0,0,$month, $day, $year);
}
else
{//We are dealing with it after 3 AM so we take today's 6 PM as start time.
$day = date("d", $today);
$month = date("m", $today);
$year = date("Y", $today);
$begintime = mktime(18, 0, 0, $month, $day, $year);
//Could have taken day+1 but doesn't works at the end of the month or end of the year.
//So just add number of seconds for 9 hours
$endtime = $begintime + 32400;//

$now = time();
if($now >= $begintime && $now <= $endtime){
    die('x');
}
else {
//Run your code that you wanted to run outside this timeframe
}
$today=strotime(“今天”);
$昨天=标准时间(“昨天”);

如果(date(“H”);>=0&&date(“H”)=$begintime&&$now时间本身就需要一个日期部分,否则无法知道“18:00”是今天、昨天还是100年后。这不是对DateTime或任何其他实现的限制,而是时间是如何工作的。如果您检查新的
DateTime('18:00')的结果;
它会将日期部分默认为当前日期

如果您希望更可靠地生成此时间段:

//如果不显式声明时区,它将使用系统时区
//这通常不是你想要的。
$tz=新日期时区(“美国/温哥华”);
$begin=新日期时间($20:00,$tz);
//7小时间隔
$span=新的日期间隔('PT7H');
$end=clone$begin;
$end->add($span);
变量转储($begin,$end);
输出:

object(DateTime)#2 (3) {
  ["date"]=>
  string(26) "2020-07-03 20:00:00.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(17) "America/Vancouver"
}
object(DateTime)#4 (3) {
  ["date"]=>
  string(26) "2020-07-04 03:00:00.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(17) "America/Vancouver"
}

时间本身就需要一个日期部分,否则无法知道“18:00”是今天、昨天还是100年后。这不是DateTime或任何其他实现的限制,而是时间是如何工作的。如果您检查新的
DateTime('18:00')的结果;
它会将日期部分默认为当前日期

如果您希望更可靠地生成此时间段:

//如果不显式声明时区,它将使用系统时区
//这通常不是你想要的。
$tz=新日期时区(“美国/温哥华”);
$begin=新日期时间($20:00,$tz);
//7小时间隔
$span=新的日期间隔('PT7H');
$end=clone$begin;
$end->add($span);
变量转储($begin,$end);
输出:

object(DateTime)#2 (3) {
  ["date"]=>
  string(26) "2020-07-03 20:00:00.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(17) "America/Vancouver"
}
object(DateTime)#4 (3) {
  ["date"]=>
  string(26) "2020-07-04 03:00:00.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(17) "America/Vancouver"
}

更正我的道歉,这是我写作中的一个错误,但由于我之前测试的地方还不是晚上8点。-如果我删除endtime检查,它仍然有效,我相信这与它一直持续到第二天早上有关。更正我的道歉,这是我写作中的一个错误,但因为我现在测试的地方还不是晚上8点,所以我认为它仍然有效如果我删除endtime检查,它仍然有效,我相信这与它进入第二天早上的事实有关。它在哪里失败?你遵循哪个测试用例?它在哪里失败?你遵循哪个测试用例?当你进入第二天时,begintime`将是第二天的
6:00 PM
8:00 PM
和nex从
00:01
03:00
的t天时间段将不起作用。只需试运行,并认为这是
1:00 AM
2:00 AM
请检查编辑的版本。我只是添加了固定的月末和年终问题。当您转到第二天时,开始时间将是第二天的
6:00 PM
8:00 PM
和第二天从
00:01
03:00
的时段将不起作用。只需试运行,并认为这是
1:00 AM
2:00 AM
请检查编辑的版本。我刚刚添加了修复月末和年终问题。