PHP - 找到下一个未来的定期日期? [英] PHP - find next future recurring date?

查看:124
本文介绍了PHP - 找到下一个未来的定期日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库的事件既静态(在特定的一天)和循环(从特定的一天开始,但每周或每隔一周设置为重复)。我明白,需要7到14的间隔时间,但是我不知道如何到达这个时间点,以便今天找到一个日期,然后吐出来。

I have a database of events that are both "static" (on a specific day) and "recurring" (starting on a specific day, but set to be "recurring" either every week or every other week). I understand that there needs to be intervals of 7 and 14, but I don't know how to get to that point to find a date > today and spit it out.

so example;

so example;

我想找到下一个即将到来的周期性事件,并吐出他们的相关日期。旁注:我遇到的数据是字符串(我知道,我知道)的Ymd所以20150821将是2015年8月21日。

I want to find the next upcoming recurring events and spit out their relevant dates. Side note: the data I'm stuck working with is in strings (I know, I know) of Ymd so 20150821 would be Aug 21st 2015.

如果今天是八月2015年11月21日,从8月7日开始的每隔一个星期五都会有一个反复发生的事件,这将是+14,这将让你今天,星期五8月21日。

if today was Aug 21 2015 and there's a recurring event for "Every other Friday" starting on Aug 7, it would be +14 which would get you to today, Friday Aug 21.

但是说从8月19日开始有一个每个星期三,我想要得到8月26日星期三的日期,并吐出来。

But say there was one for "Every Wednesday" starting Aug 19, I'd want to get the date for Wednesday, Aug 26 and spit that out.

在未来的无限日期工作,起始日期永远不会改变。

This would need to work for infinite dates in the future, with the start dates never changing.

所以在2016年1月1日运行脚本,我需要知道下一个每个星期三是2016年1月6日。

So running the script on Jan 1 2016, I'd need to know that the next "Every Wednesday" was Jan 6 2016.

伪代码:

if(start_date < today_date) {
    // start date is in the past
    add recurring_inverval to start_date until result >= today_date
    echo result
} elseif(start_date > today_date {
    // start date is in the future
    echo start_date
}

这是添加,直到我迷路了。不知道如何在if语句中这样做。

it's the adding until x that I'm lost at. not sure how to do that within an if statement.

也不知道这是否是最好的方法。我知道PHP也可以做复杂的字符串并将它们转换为日期。像下个星期六一样的

also not sure if that's the best way to go about it. I know PHP can also do complicated strings and convert them to a date. like "Next Saturday"

推荐答案

我知道你自己回答了这个问题,但另一个选择是使用模数)减去开始日期,以计算您的下一个日期。这是一个简单的脚本:

I know you answered this yourself, however another option is to just use the modulus (remainder after devision) of the current date minus the start date to compute your next date. Here is a quick script to do just that :

<?php
function nextDate($start_date,$interval_days,$output_format){
    $start = strtotime($start_date);
    $end = strtotime(date('Y-m-d'));
    $days_ago = ($end - $start) / 24 / 60 / 60;
    if($days_ago < 0)return date($output_format,$start);
    $remainder_days = $days_ago % $interval_days;
    if($remainder_days > 0){
        $new_date_string = "+" . ($interval_days - $remainder_days) . " days";
    } else {
        $new_date_string = "today";
    }
    return date($output_format,strtotime($new_date_string));
}
echo nextDate('20151210',14,'Ymd') . "<br />";
echo nextDate('20150808',14,'Ymd') . "<br />";
?>

如果开始日期在遥远的位置,您也不想退回早期未来。代码更新以防止。

You also don't want to return an early date if the "start date" is in the distant future. Code updated to prevent that.

这篇关于PHP - 找到下一个未来的定期日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆