PHP 查找最接近时间线期间的日期

发布于 2024-10-08 18:49:53 字数 644 浏览 0 评论 0原文

所以,呃,好吧。这可能会涉及到数学问题,所以希望您随身携带科学计算器;)

这是我的问题:

alt text

给定一个初始值日期(时间戳)、时间段(秒)和今天的日期(时间戳),我需要找到与周期*n加上原始/初始日期一致的最近日期。

到目前为止,我得到了一些运行良好的东西,例如初始日期和最终日期(今天的)之间的“周期”数量,在上面的演示中为“2”:

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

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

我做的下一件事是:

$range=$periods*$period;

最后

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

:写道“2011 年 4 月 3 日”。怎么到3了? (我怀疑这是闰年问题?) 你知道当你错过一些小事时的感觉吗?我现在就感觉这一切都在我身上......

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心是晴朗的。 2024-10-15 18:49:53

好吧,如果我明白你在问什么,你想知道在给定时间段内将发生的下一个日期(在你的情况下,从 1991 年 4 月 2 日开始每 10 年,下一个日期是什么时候:2011 年 4 月 2 日) )。

因此,您应该更深入地了解 PHP 中的 DateTime 类wayyyy 更适合用于日期,因为它更准确。您可以将其与完全符合您需要的 DateInterval 混合:

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

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 !
?>
土豪 2024-10-15 18:49:53

试试这个:

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

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

echo date('d M Y', $current);

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);
帝王念 2024-10-15 18:49:53

发生了什么:

0年(1970年)开始的+10年将包括3个闰年'72、'76和'80,但从'91到'11只有五个闰年'92、'96、'00、'04 和'08。您将该期间添加了两次,因此由于没有 6 个闰年,因此您多了一天。

您需要做什么:

使用 strtotime 一次一步地广告周期。

$period = "+10 years";
$newTime = $startingTime;
while(<condition>){
    $newTime = strtotime($period, $newTime);
}

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);
}
牵你的手,一向走下去 2024-10-15 18:49:53

作为对 cx42net 的答案的修复:

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

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文