Welcome to WebmasterWorld Guest from 54.159.214.27

Forum Moderators: coopster & jatar k

php convert string to date time problem

php strtotime function issue

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

5+ Year Member



Hi

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.

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

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



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 Jul 11, 2011 (gmt 0)

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



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().
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month