Driven around the bend by 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
elfranko
Forum Newbie
Posts: 4
Joined: Fri Jul 25, 2008 4:24 am

Driven around the bend by strtotime()

Post 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
Paul Arnold
Forum Contributor
Posts: 141
Joined: Fri Jun 13, 2008 10:09 am
Location: Newcastle Upon Tyne

Re: Driven around the bend by strtotime()

Post 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.
elfranko
Forum Newbie
Posts: 4
Joined: Fri Jul 25, 2008 4:24 am

Re: Driven around the bend by strtotime()

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Driven around the bend by strtotime()

Post 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.
(#10850)
elfranko
Forum Newbie
Posts: 4
Joined: Fri Jul 25, 2008 4:24 am

Re: Driven around the bend by strtotime()

Post 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
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Driven around the bend by strtotime()

Post 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.
User avatar
EverLearning
Forum Contributor
Posts: 282
Joined: Sat Feb 23, 2008 3:49 am
Location: Niš, Serbia

Re: Driven around the bend by strtotime()

Post 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
Post Reply