只有PHP strtotime的未来日期()

PHP's strtotime() uses the current year by default. How to get only future dates?

echo date('l d. M Y', strtotime('first sunday of april')); // Sunday 03. Apr 2016

I can't manage to get the next first Sunday of April. The date must not be in the past and it must be always relative (no 2017 or 2018 hardcoded).

echo date('l d. M Y', strtotime('first sunday of next april')); // fails

echo date('l d. M Y', strtotime('first sunday of april next year')); // wrong from January until that Sunday in April

I think I could do it in multiple steps or create a function to check, if current time is before/after the first Sunday and insert a 'next year' at the end.

But I was wondering if there is a simple solution with strtotime()

I dont think this is particularly elegant, but it works, and I hope it is what you were looking for?

echo date('l d. M Y', strtotime('first sunday of april', strtotime('first day of next year')));

However this seems like a much better, maintainable, readable solution

$d = new DateTime();
$d->modify( 'first day of next year');
echo $d->format('l d. M Y') . PHP_EOL;
$d->modify( 'first sunday of april');
echo $d->format('l d. M Y') . PHP_EOL;

Which gives

Tuesday 01. Aug 2017
Sunday 02. Apr 2017

The echo of the year change date, you would not need to do, its there just to prove that the year changed

I came here looking for a solution to the title of this post. I wanted to get the future date for a strtotime result every time.

date("Y-m-d", strtotime("Jan 2"));

If today's date is Jan 1, 2018, it will return the future date of 2018-01-02, but if today's date is Jan 3, 2018, it will return the same date (which is now in the past). In that case, I would prefer to get back 2019-01-02.

I know the OP said they didn't want a function, but that seemed like the simplest solution. So, here's the quick function I made to get the next future strtotime that matches.

function future_strtotime($d){
    return (strtotime($d)>time())?strtotime($d):strtotime("$d +1 year");
}

Get that good date using...

date("Y-m-d", future_strtotime("Jan 2"));

Here's a more robust solution. It replaces strtotime but requires a second parameter -- a string of either past or future and offsets according.

<?php
function strtotimeForce($string, $direction) {
    $periods = array("day", "week", "month", "year");
    if($direction != "past" && $direction != "future") return strtotime($string);
    else if($direction == "past" && strtotime($string) <= strtotime("now")) return strtotime($string);
    else if($direction == "future" && strtotime($string) >= strtotime("now")) return strtotime($string);
    else if($direction == "past" && strtotime($string) > strtotime("now")) {
        foreach($periods as $period) {
            if(strtotime($string) < strtotime("+1 $period") && strtotime($string, strtotime("-1 $period"))) {
                return strtotime($string, strtotime("-1 $period"));
            }
        }
        return strtotime($string);
    }
    else if($direction == "future" && strtotime($string) < strtotime("now")) {
        foreach($periods as $period) {
            if(strtotime($string) > strtotime("-1 $period") && strtotime($string, strtotime("+1 $period")) > strtotime("now")) {
                return strtotime($string, strtotime("+1 $period"));
            }
        }
        return strtotime($string);
    }
    else return strtotime($string);
}