3

When I run the following code, I get the 30th of April rather than the 27th.

strtotime("Last Friday April 2012");

I tried running it as a Thursday instead and I got back the 29th.

All the following work fine.

strtotime("First Sunday February 2012");
strtotime("Third Monday February 2012");
strtotime("Second Monday October 2012");
strtotime("Fourth Thursday November 2012");

Any ideas?

Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
NRoscoe
  • 33
  • 5
  • strtotime is a wonderful example of garbage-in, garbage-out - your phrase is not specific enough, so strtotime guess wrong. – Marc B Feb 12 '12 at 04:45

2 Answers2

9

I believe you are getting back the 30th of March, rather than of April, correct?

Last Friday April 2012 does the following:

  • April 2012 sett the date to 2012-04-01
  • Last Friday takes the date to the previous (last) Friday which is 2012-03-30

If you want the last Friday in April, use Last Friday May 2012 instead,

See Date/Time - Relative Formats from the PHP manual.

drew010
  • 68,777
  • 11
  • 134
  • 162
4

You can also use:

strtotime("last friday of April 2012");

echo date( 'F jS, Y h:i:s A', strtotime("last friday of April 2012"));

This outputs the desired date:

April 27th, 2012 12:00:00 AM
nickb
  • 59,313
  • 13
  • 108
  • 143