开发者

strtotime - always return NEXT [date here]

I'm trying to use the strtotime function in order to return 开发者_StackOverflow中文版the following date specified. For example:

strtotime('thursday'); //Need to get NEXT Thurs, not today (assuming today is Th)

Using next thursday would work in this case, but does not work with absolute dates, such as:

strtotime('next aug 19'); //Should return 08-19-2011, but instead returns 12-31-1969

I'm thinking this may call for some regexes, but since the formats that will be input are so variable, I'd rather find a simpler solution if it exists.

Thanks!


There's no way that I'm aware of. But you can ask for the current year and then add 1 year if necessary:

$d = new DateTime("Aug 19");
if (new DateTime() >= $d) {
    $d->add(new DateInterval("P1Y"));
}
echo $d->format(DateTime::W3C);


Can you not use the second parameter of the strtotime method which is a date value to give you a basis for the calculations instead of today? So you could set that to august 19 and then put in some calc for +1 year.


asking the wrong question will result in the wrong answer. 'next aug 19' could be next year, next millennium, ...

try

strtotime('+1 year aug 19'); //or strtotime('+1 week aug 19');

happy coding


Here's what I came up to. Credit goes to @Artefacto for helping come up with the solution.

$interval = 'thursday';
$oldtime = strtotime('now');
$newtime = strtotime($interval);
$i = 0;
while ($oldtime >= $newtime) {
    $newtime = strtotime($interval, strtotime("+" . $i*$i . " hours"));
    $i++;
}

echo date("Y-m-d h:i:s", $newtime);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜