tags:

views:

138

answers:

5

Going through a weird problem here.

I am trying to get the date of coming March, but it doesn't seem to work.

$nextMarch = strtotime("next march");

What could be wrong with this tiny little code?

This is a question related to this question:
http://stackoverflow.com/questions/1872812/timestamp-of-nearest-valid-month

A: 

strtotime() is not that smart. You can only do weekdays with next or last.

ZZ Coder
PHP 4.4.0 does yield 1235862000 which is 2009-01-01 in GMT+1 (using localtime is a bit stupid but maybe that's just one obscure PHP option)
Joey
Really? What does "next month" give you? You may be (un)pleasantly surprised.
paxdiablo
+6  A: 

I think you'll have to give strtotime some other (and/or additional) information.

For instance, the number of the day you want in march :

$nextMarch = strtotime("1 march");
var_dump($nextMarch, date('Y-m-d H:i:s', $nextMarch));

But you'll get march this year :

int 1235862000
string '2009-03-01 00:00:00' (length=19)

So, to get "next" march, i.e., next year :

$nextMarch = strtotime("1 march +1 year");
var_dump($nextMarch, date('Y-m-d H:i:s', $nextMarch));

And you get :

int 1267398000
string '2010-03-01 00:00:00' (length=19)

So, maybe you'll have to do some calculation by yourself, to know whether you want this year or the next one. Something like this might do the trick:

$nextMarch = strtotime("1 march");
if ($nextMarch < time()) {
  $nextMarch = strtotime("1 march +1 year");
}
var_dump($nextMarch, date('Y-m-d H:i:s', $nextMarch));

(I don't really like this idea, but it seems to be working -- even though a simpler solution would definitely be nice...)

Pascal MARTIN
You would actually need a counter variable for this, right? Something like strtotime("1 march +"+year_counter+" year");
Roman Stolper
Slightly simpler: $yr = 'this'; if (time() >= strtotime('March 1st')) { $yr = 'next'; } echo date('Y-m-d', strtotime($yr . ' year March 1st'));
GZipp
Thank you for that piece of explanation! Sometimes we believe that strtotime is more intelligent.
Nirmal
A: 

What do you mean 'date of coming March'? March is a collection of 31 dates (one for each day).

Anyway, this example of strtotime() (from the online Manual) uses 'next month', to return "the last day of the current month"

It might be worth a look.

pavium
That example goes a bit overboard. The last day of the month can be gotten with *date('t')*.
GZipp
Yes, but it shows, at least, that 'next month' is valid (which wasn't my first impression), and I thought it could be adapted.
pavium
A: 

I'd be inclined to do more mangling myself and rely less on strtotime:

$thisYear = date("Y");
$nextYear = $thisYear + 1;
$nextMarch = date($yourFormat, strtotime("March 1, $nextYear"));
antik
+1  A: 

Using strtotime here seems a bit odd and weird for me. I'd prefer using mktime for this.

mktime (0,0,0,3,1,date("Y")+1);
Saulius Lukauskas