PHP Find date nearest to a timeline period

2020-07-10 08:27发布

So, uh, ok. This might get mathematical, so hope you brought your scientific calculator with you ;)

This is my problem:

alt text

Given an initial date (timestamp), time period period (seconds) and today's date (timestamp), I need to find the nearest date which coincides with the period*n plus the original/initial date.

So far, I got some stuff working nicely, such as the amount of "periods" between the initial and final(today's) date, which would be "2" in the demo above:

$initial=strtotime('2 April 1991');
$time=time();
$period=strtotime('+10 years',0);

$periods=round(($time-$initial)/$period);

The next thing I did was:

$range=$periods*$period;

And finally:

echo date('d M Y',$initial+$range);

Which wrote '03 April 2011'. How did it get to 3? (I suspect it's a leap year issue?) You know that feeling when you're missing something small? I'm feeling it all over me right now....

4条回答
Summer. ? 凉城
2楼-- · 2020-07-10 08:41

Try this out:

$current = $initial = strtotime('2 April 1991');
$time_span = '+10 years';

while ($current < time())
{ 
  $current = strtotime($time_span, $current);
}

echo date('d M Y', $current);
查看更多
我欲成王,谁敢阻挡
3楼-- · 2020-07-10 08:48

What happened:

+10 years from Year 0 (1970) will include 3 leap years '72, '76 and '80, but from '91 till '11 there are only five leap years '92, '96, '00, '04 and '08. You added that period twice, so because there weren't 6 leap years you got one extra day.

What you need to do:

Ad the period with strtotime one step at a time.

$period = "+10 years";
$newTime = $startingTime;
while(<condition>){
    $newTime = strtotime($period, $newTime);
}
查看更多
手持菜刀,她持情操
4楼-- · 2020-07-10 08:51

Ok so if I understood what you are asking, you want to know the next date that will occurs in a given period of time (in your case, every 10 years starting from 2 April 1991, when will be the next date : 2 april 2011).

So, you should take a deeper look at the DateTime class in PHP that is wayyyy better to use for the dates because it is more accurate. You mix it with DateInterval that match exactly what you need :

<?php
$interval = new DateInterval('P10Y'); // 10 years
$initial = new DateTime('1991-04-02');
$now = new DateTime('now');

while ($now->getTimestamp() > $initial->getTimestamp()) {
    $initial = $initial->add($interval);
}

echo $initial->format('d M Y'); // should return April 2, 2011 !
?>
查看更多
在下西门庆
5楼-- · 2020-07-10 08:56

As a fix to cx42net's answer:

<?php

$initial = new DateTime('2 April 1991');
$now = new DateTime('now');
$interval = new DateInterval('P10Y');

$curDate = $initial;

while (true) {
    $curDate = $curDate->add($interval);

    $curDiff = $curDate->diff($now)->days;

    if (isset($lastDiff) && ($curDiff > $lastDiff)) {
        echo $lastDate->format('d M Y');
        break;
    } else {
        $lastDate = clone $curDate;
        $lastDiff = $curDiff;
    }
}
查看更多
登录 后发表回答