Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

php convert string to date time problem

php strtotime function issue

7:27 am on Jul 9, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:May 11, 2009
posts: 74
votes: 0


I've to convert string to date time. I'm doing the following:

$ndate= date("m-d-Y", strtotime($oDate))." ".date("H:i:s", strtotime($oTime));

$dated = date("Y-m-d H:i:s", strtotime($ndate));

First I have converted the posted date and time from string to datetime.
Then I have changed it's format for mySQL.

All this is working perfect on one of my page, but not working on the second one, and returns the date 01-01-1970.

I've checked in detail, but was unable to understand. Can there be alternative for it. I have to get date and time separately from the input form.

11:33 am on July 9, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 0

On your second page that returns 01-01-1970, what does $oDate and $oTime contain? I would guess there is a problem with your form (or how you are reading the values from it).

You seem to be doing an excessive amount of (unnecessary) converting. Your first statement does the same as your second statement if you converted to "Y-m-d" format to begin with. What is preventing you from doing this in 1 statement?
$dated = date("Y-m-d H:i:s", strtotime($oDate.' '.$oTime));

But if your goal is to build a MySQL query then may be you should look at MySQL's FROM_UNIXTIME [dev.mysql.com]?
4:21 pm on July 11, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
votes: 0

The **reason** you get 01-01-1970 is that this is the beginning of the "Unix epoch" and you will always get this result if an invalid date is passed to most date functions - money's on penders' assessment. :-) Skip all that and use from_unixtime().