Php 确定日期时间是否在时间范围内的最有效方法

Php 确定日期时间是否在时间范围内的最有效方法,php,datetime,datetime-format,Php,Datetime,Datetime Format,我有一份工作,每5分钟一次。它应该只在一天中的特定时间(如早上和晚上)执行某些任务 php最有效/最优雅的方法是什么来确定现在的日期时间是否在cronjob可能启动的5分钟时间范围内 目前我正在做: $date = new DateTime(); $hour = (int)$date->format('H'); $min = (int)$date->format('i'); if($hour == 7 && ($min >= 40 || $min <

我有一份工作,每5分钟一次。它应该只在一天中的特定时间(如早上和晚上)执行某些任务

php最有效/最优雅的方法是什么来确定现在的日期时间是否在cronjob可能启动的5分钟时间范围内

目前我正在做:

$date = new DateTime();

$hour = (int)$date->format('H');
$min = (int)$date->format('i');

if($hour == 7 && ($min >= 40 || $min < 45)) {
    // Do something in the morning
}

if($hour == 21 && ($min >= 00 && $min < 05)) {
    // Do something in the evening
}

作为本机php代码?

如果
$begin
$end
也属于DateTime类型,您可以这样简单地使用它们:

if ($begin <= $date && $date <= $end) {
    // .. date is within the range from $begin -> $end ..
if($begin setTime($hours,$minutes);
$end=clone$begin;
$end->modify('+'.intval($timerangeInMinutes)。'minutes');

return($begin如果
$begin
$end
也是DateTime类型,您可以这样简单地使用它们:

if ($begin <= $date && $date <= $end) {
    // .. date is within the range from $begin -> $end ..
if($begin setTime($hours,$minutes);
$end=clone$begin;
$end->modify('+'.intval($timerangeInMinutes)。'minutes');

return($begin您可以使用UNIX时间(从1970年1月1日起的秒数,也称为Epoch)

<?php
$current_time = time(); //Get timestamp
$cron_time = (int) ;// Time cron job runs (you can use strtotime() here)
$five_minutes = 300; //Five minutes are 300 seconds 

if($current_time > $cron_time && $current_time - $cron_time >= $five_minutes) {
echo "Cron Job is too late";
} elseif($current_time >= $cron_time && $five_minutes >= $current_time - $cron_time){
echo "Cron Job ran within time frame";
}

?>

您可以改用UNIX时间(从1970年1月1日起的秒数,又称纪元)。然后,逻辑应该如下所示

<?php
$current_time = time(); //Get timestamp
$cron_time = (int) ;// Time cron job runs (you can use strtotime() here)
$five_minutes = 300; //Five minutes are 300 seconds 

if($current_time > $cron_time && $current_time - $cron_time >= $five_minutes) {
echo "Cron Job is too late";
} elseif($current_time >= $cron_time && $five_minutes >= $current_time - $cron_time){
echo "Cron Job ran within time frame";
}

?>

您可以扩展DateTime以向其中添加您自己的方法。我会这样做:-

class MyDateTime extends DateTime
{
    /**
    * Checks if this DateTime is between two others
    * @param DateTime $start
    * @param DateTime $end
    * @return boolean 
    */
    public function inRange(DateTime $start, DateTime $end){
        return ($this >= $start && $this <= $end);
    }
}

这是我能想到的最干净的方法,也是你要求的方法。

你可以延长DateTime来添加你自己的方法。我会这样做:-

class MyDateTime extends DateTime
{
    /**
    * Checks if this DateTime is between two others
    * @param DateTime $start
    * @param DateTime $end
    * @return boolean 
    */
    public function inRange(DateTime $start, DateTime $end){
        return ($this >= $start && $this <= $end);
    }
}

这是我能想到的最干净的方法,也是你要求的方法。

是否可以将day设置为“每年、每月和每天”?因为我希望每天都运行它……如果每次都必须初始化依赖的begin和end,代码就会膨胀(因此,我也可以使用当前版本)。您可以这样做:
$begin=new DateTime();$begin->setTime(7,40);
-这将为您提供一个表示当天7:40的DateTime实例。但这不是最有效的解决方案。是否可以将日间设置为“每年、每月和每天”?因为我想每天都运行它…如果每次都必须初始化依赖的开始和结束,它会使代码膨胀(因此,我也可以使用当前版本)。您可以这样做:
$begin=new DateTime();$begin->setTime(7,40)
-这将为您提供一个表示当天7:40的DateTime实例。但这并不是最有效的解决方案。我更喜欢使用对象。然而,此代码与我的代码一样复杂:)但它更具适应性,因为您可以在任何时间使用,而无需为每个cron作业(即从您的帖子:$hour==21&)重写代码($min>=00&&$min<05)。因此,如果您想每天运行10个cron作业,您只需要这一小部分。我更喜欢使用对象。但是,这段代码和我的代码一样复杂:)它更具适应性,因为您可以随时使用,而无需为每个cron作业重写代码(即,从您的帖子:$hour==21&($min>=00&$min<05)所以,如果你想每天运行10个cron作业,你只需要这个小片段。