简体   繁体   English

如何在不指定年份的情况下查找下一个最接近的日期(月/日/时间)

[英]how to find the next closest date (month/day/time) without specifying year

I have a script that a user can setup a frequency as to when they want it to run. 我有一个脚本,用户可以设置运行频率。

They specify 他们指定

  • The month they want it to run 他们希望它运行的月份
  • The day of the month 一个月中的某天
  • The time (24 hour format) 时间(24小时格式)

They do not specify the year, the script needs to find the next closest date that matches. 它们未指定年份,脚本需要查找下一个最接近的日期。

PHP PHP

The way I propigate the dates is through an array of months the user chooses and I can output all the dates into a nice array 我预测日期的方式是通过用户选择的月份数组,我可以将所有日期输出到一个不错的数组中

$scheduler_months = unserialize($row['scheduler_months']);
foreach ($scheduler_months as $scheduler_month) {
    $next_date[] = $scheduler_month."/".$row['scheduler_date']." ".$row['scheduler_time'];
}

which will out put 这将被放出来

Array ( [0] => 2/28 12:00 [1] => 4/28 12:00 [2] => 12/28 12:00 )

So now at this point I need to figure out what the next closest date based on today as the starting point will be, obviously if the next closest date is in the next year it needs to be smart enough to figure that out. 因此,现在在这一点上,我需要确定基于今天的下一个最接近的日期是什么起点,显然,如果下一个最接近的日期是在明年,那么它需要足够聪明才能弄清楚。 I just have no idea how to find the next closest date based on the dates in the array. 我只是不知道如何根据数组中的日期查找下一个最接近的日期。

It's quite easy to figure out the year of the dates - if you were to convert them to unix time using strtotime you can determine if this year's date was in the past or not and if it were, you can assign that date next year. 确定日期的年份非常容易-如果要使用strtotime将日期转换为Unix时间,则可以确定今年的日期是否是过去的日期,如果是,则可以指定明年的日期。

$scheduler_months = unserialize($row['scheduler_months']);
$now = strtotime("now"); # get the unix time in seconds 'now'
foreach ($scheduler_months as $scheduler_month) { 
    # $tmp will be holding the date in the form of YYYY-MM-DD HH:MM
    $tmp = date("Y")."-".$scheduler_month."-".$row['scheduler_date']." ".$row['scheduler_time'];
    if(strtotime($tmp) - $now < 0) # if date is in the past, assign it to the next year
            $tmp = (date("Y")+1)."-".$scheduler_month."-".$row['scheduler_date']." ".$row['scheduler_time'];
    $next_date[] = $tmp;
}

# Initialize $smallest and $smallest_key
$smallest = strtotime($next_date[0])-$now;
$smallest_key = 0;

foreach($next_date as $key => $val) {
        $time_diff = strtotime($val) - $now;
        if($time_diff < $smallest) {
                $smallest_key = $key;
                $smallest = $time_diff;
        }
}

In the first part, I've modified your foreach loop to determine the correct year based on unix time. 在第一部分中,我修改了您的foreach循环,以根据Unix时间确定正确的年份。 I've changed the date format to YYYY-MM-DD HH:MM . 我已将日期格式更改为YYYY-MM-DD HH:MM If the date's unixtime is less than the current unix time, then the next closest date is next year. 如果日期的unixtime小于当前的unix时间,则下一个最接近的日期是明年。

In the second part I initilize two variables - $smallest , which holds the smallest time in seconds relative to now and $smallest_key , which holds the array key for the smallest time. 在第二部分中,我初始化了两个变量- $smallest$smallest_key ,该变量以相对于现在的最小时间为单位,而$smallest则以最小的时间保持数组的键。

Then I loop over $next_date and I look for the smallest time in seconds from now to either of the dates. 然后,我遍历$next_date并寻找从现在到两个日期中最小的时间(以秒为单位)。

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

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