PHP Find date nearest to a timeline period

≯℡__Kan透↙ 提交于 2019-12-12 08:47:15

问题


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

This is my problem:

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....


回答1:


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 !
?>



回答2:


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:


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:


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;
    }
}


来源:https://stackoverflow.com/questions/4509115/php-find-date-nearest-to-a-timeline-period

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!