homepage Welcome to WebmasterWorld Guest from 50.17.86.12
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
php convert string to date time problem
php strtotime function issue
abidshahzad4u

5+ Year Member



 
Msg#: 4337319 posted 7:27 am on Jul 9, 2011 (gmt 0)

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

 

penders

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



 
Msg#: 4337319 posted 11:33 am on Jul 9, 2011 (gmt 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]?

rocknbil

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



 
Msg#: 4337319 posted 4:21 pm on Jul 11, 2011 (gmt 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().

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved