I would like to get the upcoming date if it has passed when using strtotime.

My code:

date('Y.m.d H:i:s', strtotime('1 January'));

That returns 2014.01.01 00:00:00. I would like it to return 2015.01.01 00:00:00 as the date has passed. If today would be January 1 it should return 2014.01.01 00:00:00.

date('Y.m.d H:i:s', strtotime('this friday')); is working just like that but I would like to somehow be able to work with dates instead of weekdays.

有帮助吗?

解决方案

function upcomingDate($target)
{
    $today = mktime(0,0,0);
    $time  = strtotime($target);
    if ($time == $today)
        return $today;
    elseif ($time < $today)
        return strtotime($target . ' next year');
    else
        return $time;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top