查找下一个日期

时间:2014-09-30 14:27:07

标签: php

我想从当前日期找到下一次出现的日期。

例如,假设我想从当前日期找到该月的第20个

如果当前日期是10月10日,则返回结果2014-10-20(Y-m-d) 如果当前日期是10月22日,则返回结果2014-11-20(Y-m-d)

3 个答案:

答案 0 :(得分:1)

我刚刚使用while循环创建了一个解决方案。

$oldd= "2014-06-20";

$newdate = date("Y-m-d",strtotime($oldd."+1months"));

while(strtotime($newdate) <= strtotime(date("Y-m-d")))
{
    $newdate = date("Y-m-d",strtotime($newdate."+1months"));
}
echo $newdate;

答案 1 :(得分:0)

手动

并将其传递给strtotime()。您需要从参考时间字符串中提取的时间信息。像这样:

$refdate = '2014-02-25 10:30:00';
$timestamp = strtotime($refdate);

echo date('Y-m-d H:i:s',
    strtotime("next Thursday " . date('H:i:s', $timestamp), $timestamp)
);

使用字符串串联可以达到相同的结果:

echo date('Y-m-d', strtotime("next Thursday", $timestamp)
    . ' ' . date('H:i:s', $timestamp);

答案 2 :(得分:0)

另一种方法,您可以使用DateTime对象的方法,PHP在处理日期时间方面确实具有丰富的API。

$current_date = new DateTime('2014-06-20');

if ($current_date->format('d') >= 20) {
    // $current_date->modify('last day of this month')->modify("+20 days");
    $current_date->modify('first day of next month')->modify("+19 days");
}else{
    $current_date->modify('first day of this month')->modify("+19 days");
}
echo $current_date->format("Y-m-d");

http://php.net/manual/en/datetime.modify.php http://php.net/manual/en/datetime.formats.relative.php

相关问题