dcp3450
dcp3450

Reputation: 11187

Why is strtotime one week off?

strtotime("third Saturday October 2011")

Should be 10/15/2011. However, it's coming up at 10/22/2011. I assume this is because October 2011 starts on a Saturday and PHP is looking at the first full week. Since 10/1/2011 is a Saturday not a full week it ignores it.

Some research suggested putting "of" between the day of the week and the month should fix it but that doesn't work.

Any suggestions on why this is happening and what I can do to correct it?

Upvotes: 0

Views: 511

Answers (3)

Rudu
Rudu

Reputation: 15892

This is a documented flaw in PHP <5.2.7 (see strtotime):

In PHP 5 prior to 5.2.7, requesting a given occurrence of a given weekday in a month where that weekday was the first day of the month would incorrectly add one week to the returned timestamp. This has been corrected in 5.2.7 and later versions.

You'll need to upgrade PHP, or use a work around like Jonathan Kuhn suggests.

Upvotes: 2

Jonathan Kuhn
Jonathan Kuhn

Reputation: 15301

I was getting the same results as you on php 5.2.6. This works for me although not ideal.

echo date('Y-m-d', strtotime('Saturday October 2011 +2 weeks'));

Upvotes: 1

ceejayoz
ceejayoz

Reputation: 180023

<?php echo date('Y-m-d', strtotime('third Saturday of October 2011')); ?>

Works fine for me. Output is 2011-10-15.

Upvotes: 0

Related Questions