Page 1 of 1
Daylight saving problem with strtotime
Posted: Tue Oct 31, 2006 5:45 am
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?
Re: Daylight saving problem with strtotime
Posted: Tue Oct 31, 2006 6:18 am
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!?
Posted: Tue Oct 31, 2006 6:33 am
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!
Posted: Tue Oct 31, 2006 7:30 am
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"))."') ";
Posted: Tue Oct 31, 2006 7:52 am
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.