NRoscoe
NRoscoe

Reputation: 33

php strtotime "last friday April 2012" not returning last friday

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?

Upvotes: 3

Views: 1611

Answers (2)

nickb
nickb

Reputation: 59699

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

Upvotes: 4

drew010
drew010

Reputation: 69957

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.

Upvotes: 9

Related Questions