Page 1 of 1
Driven around the bend by strtotime()
Posted: Thu Aug 07, 2008 3:54 am
by elfranko
Hi all - this function is driving me completely around the bend - can anyone please help?!
I have a couple of drop down boxes in a form - From: day/month/year To: day/month/year
I capture these with a $_POST[] and then run them through strtotime as:
Code: Select all
$from = $fromYear."-".$fromMonth."-".$fromDay." 00:00:00";
$to = $toYear."-".$toMonth."-".$toDay." 23:59:59";
$fromDate = strtotime($from);
$toDate = strtotime($to);
echo $fromDate."<br/>";
echo $toDate."<br/>";
The times are put into two DB fields. They and the echo output I am checking on a couple of online unix date stamp conversion sites and I keep loosing an hour!! I know its to do with stupid BST/GMT and all that - I have even tried to use
Code: Select all
//timezone set
date_default_timezone_set(Europe/London);
to no avail. Every time, the conversion comes up: Fri, 19 Dec 2008 01:00:00 GMT or something like Sun, 28 Dec 2008 00:59:59 GMT for example.
Please help someone!!
thanks
frank
Re: Driven around the bend by strtotime()
Posted: Thu Aug 07, 2008 4:19 am
by Paul Arnold
Try this:
Code: Select all
$from = $fromYear."-".$fromMonth."-".$fromDay." 00:00:00";
$to = $toYear."-".$toMonth."-".$toDay." 23:59:59";
$fromDate = strtotime(date("Y-m-d H:i:s", $from));
$toDate = strtotime(date("Y-m-d H:i:s", $to));
echo $fromDate."<br/>";
echo $toDate."<br/>";
Edit: On re-reading your post I don't think this will make any difference but give it a try.
Re: Driven around the bend by strtotime()
Posted: Thu Aug 07, 2008 4:35 am
by elfranko
Thanks Paul - I tried your suggestion
Code: Select all
$from = $fromYear."-".$fromMonth."-".$fromDay." 00:00:00";
$to = $toYear."-".$toMonth."-".$toDay." 23:59:59";
$fromDate = strtotime(date("Y-m-d H:i:s", $from));
$toDate = strtotime(date("Y-m-d H:i:s", $to));
echo $fromDate."<br/>";
echo $toDate."<br/>";
exit;
And the echos were
2008
2009
Any reason why??
thanks
frank
Re: Driven around the bend by strtotime()
Posted: Thu Aug 07, 2008 4:44 am
by Christopher
You need to read the documentation more carefully:
http://us3.php.net/manual/en/function.date.php
http://us3.php.net/manual/en/function.strtotime.php
Both date() and strtotime() return a Unix timestamp (which is the integer seconds since 1970-01-01 00:00:00). The second parameter in strtotime() is also a timestamp that is use as the base time when the first string parameter contains a relative time like "+1 week". You really don't need to call these functions at all. You already have the year, month and day. Just use number format.
Re: Driven around the bend by strtotime()
Posted: Thu Aug 07, 2008 5:01 am
by elfranko
Hi arborint - thanks for the help - perhaps if I gave a little more insight as to the purpose of the code that may help?
Badically a client wants to insert into the backend to from dates for a booking service. The user then selects a place they want to stay and from which date to which date they want to stay there. I have coded my sql to to basically say get a row where placeId = the place where they want to stay and the from field is => than the requested from date and the to date is < than the todate field, so basically my code is trying to bracket a price within a given set of dates. The easiest way I could see of doing this was to convert my $_post dd/mm/yy into a unix time stamp and send the two converted dates into the db. But the problem I'm having is my back end editor is inserting data incorrectly.
If I get a timecode from the db, a random one and check it with an online convertor, the time is always either 01:00:00 instead of 00:00:00 or 00:59:59 instead of 23:59:59!!
Yet, when I open up my booking editor, so I can adjust dates, prices etc, I get back all of the rows with the unix stamps converted into the right times.
I'm really confused.
thanks
frank
Re: Driven around the bend by strtotime()
Posted: Thu Aug 07, 2008 5:45 am
by onion2k
Stop doing anything with the dates in PHP. Do it all in the database with SQL (using the DATE() and DATE_FORMAT() with DATE_ADD() and DATE_SUB() if necessary). That way you'll never need to consider times, and you'll never need to use timestamps.
If you really don't want to use SQL though, try using mktime() instead of strtotime(). mktime() has an argument at the end to make it ignore daylight savings.
Re: Driven around the bend by strtotime()
Posted: Thu Aug 07, 2008 5:48 am
by EverLearning
Make sure that the PHP timezone setting is the same as the MySql timezone setting. It can create a lot of headaches if they're not the same.
PHP manual date_default_timezone_set()
MySQL Server Time Zone Support