views:

36

answers:

4

I'm trying to use the strtotime function in order to return 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!

A: 

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.

spinon
A: 

Hi Ian, 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

silverskater
+1  A: 

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);
Artefacto
Oh, that's a good idea. Check if it's the same date, if so, add time accordingly and then recalculate. My question may not have been clear, but I need something that would work with both a single day or a month or a year. Anything that could possibly be input into strtotime. Thanks!
Ian Silber
@Ian Well, I can't think of any general approach. for week days and months use your current strategy and for month days use this.
Artefacto
Thanks - see my resolution below if interested.
Ian Silber
A: 

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);
Ian Silber