Daylight saving problem with strtotime

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ska
Forum Commoner
Posts: 41
Joined: Mon Sep 05, 2005 4:54 pm

Daylight saving problem with strtotime

Post by ska »

Hi all,

Just come across this problem, which I think looks like a bug with the version of PHP that is running our server (PHP 4.4.2).

Code: Select all

echo date("Y-m-d", strtotime("Monday -1 week"));
Today is Tuesday 31st October. And running the above code today returns

2006-10-29

Which is incorrect - the 29th was Sunday. If I run the above code on another server which is running PHP5, it works correctly and returns;

2006-10-30

I read that there are some bugs that might cause this to happen in earlier versions of PHP - couldn't find a simple workaround though. Any ideas?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Re: Daylight saving problem with strtotime

Post by JayBird »

ska wrote:Hi all,

Just come across this problem, which I think looks like a bug with the version of PHP that is running our server (PHP 4.4.2).

Code: Select all

echo date("Y-m-d", strtotime("Monday -1 week"));
Today is Tuesday 31st October. And running the above code today returns

2006-10-29

Which is incorrect - the 29th was Sunday. If I run the above code on another server which is running PHP5, it works correctly and returns;

2006-10-30

I read that there are some bugs that might cause this to happen in earlier versions of PHP - couldn't find a simple workaround though. Any ideas?
i think you will find the output above is actually correct

Code: Select all

echo date("Y-m-d", strtotime("Monday -1 week"));
Actually means....Next Monday minus 1 week

To get the expected output, you need

Code: Select all

echo date("Y-m-d", strtotime("Last Monday -1 week"));
i don't understand why you think the output should be "2006-10-30" when you are subtracting a week!?
ska
Forum Commoner
Posts: 41
Joined: Mon Sep 05, 2005 4:54 pm

Post by ska »

Thanks for the reply. Actually, I think I've pasted the wrong bit of code in there, sorry.

My overall aim is to return staff bookings in a given week.

Code: Select all

$sql = "SELECT * FROM staff_bookings WHERE (start >= '".date("Y-m-d", strtotime("last Monday +".$_GET['weeks']." week"))."' AND start <= '".date("Y-m-d", strtotime("next Monday +".$_GET['weeks']." week"))."') ";
$_GET['weeks'] will be an offset like +1, +2 or -1, -2 etc.

This has worked fine up until now when the clocks changed. Specifically, if the offset is -1, it returns bookings between Monday 23rd Oct and Saturday 28th Oct but skips the 29th. Then on offset=0, it starts at Mon 30th Oct. Sunday just gets missed!
ska
Forum Commoner
Posts: 41
Joined: Mon Sep 05, 2005 4:54 pm

Post by ska »

Fixed! Just add 12:00 after the name of the day;

Code: Select all

$sql = "SELECT * FROM staff_bookings WHERE (start >= '".date("Y-m-d", strtotime("last Monday +".$_GET['weeks']." week"))."' AND start <= '".date("Y-m-d", strtotime("next Monday 12:00 +".$_GET['weeks']." week"))."') ";
User avatar
xpgeek
Forum Contributor
Posts: 146
Joined: Mon May 22, 2006 1:45 am
Location: Kyiv, Ukraine
Contact:

Post by xpgeek »

That do you know about sql injections?
You need to validate EGPCS before use them in sql statements or in output or in something else.
Post Reply