Brian Hicks
Brian Hicks

Reputation: 6413

Make strtotime() generate times only in the future

I'm trying to make a PHP script to find the next occurence of a date (say 5-1). However, if I put that into strtotime() on 5-2 it comes up with the date from this year. I simply want to make it always return the next date possible. How can I make this work? I'm using codeigniter if that helps.

EDIT: Here's some code I came up with, if some humble soul runs across the same problem:

if (strtotime($date) < strtotime("now")) {
    $realseconds = strtotime($date) + 31556926; //add one year in seconds
} else {
    $realseconds = strtotime($date);
}

Upvotes: 0

Views: 715

Answers (4)

Quamis
Quamis

Reputation: 11087

Code:

$x=strtotime($inputString);
$YEAR=60*60*24*30*12;
while($x<$time()) $x+=$YEAR;

Basically, it adds one year if the date returned by strtotime is in the past... because i used while() it will never return a date in tha past even if it was explicitly stated like that

Upvotes: 0

Pekka
Pekka

Reputation: 449605

Assuming 5-2 means february fifth, you could do

strtotime("february 5 +1 year")

Upvotes: 1

SLaks
SLaks

Reputation: 887767

You could check whether the date returned is earlier than the current time, and, if it is, add one year to it.

You could also pass some date in the next year as the second parameter to strtotime.

Upvotes: 2

Alix Axel
Alix Axel

Reputation: 154603

What is 5-1 or 5-2?!

Try doing this instead:

strtotime('next October');

Upvotes: 1

Related Questions