warpdesign Posted September 12, 2006 Share Posted September 12, 2006 I'm reading dates from a database, they are in the format "Y-m-d H:i:s" and I'm reformating them for updating from the user using this function[code]function inputTime($timestamp){$timestamp = strtotime($timestamp);$formated = date("M/d/Y h:i A", $timestamp);return $formated;}[/code]OK that works fine. Then when I write them back to the database I want to convert them back to the original format using this function[code]function writeTime($timestamp){$timestamp = strtotime($timestamp);$formated = date("Y-m-d H:i:s", $timestamp);return $formated;}[/code]So the problem is, when I do that all my dates come out as Wed, December 31st 1969, 3:59 PM?? ??? Link to comment https://forums.phpfreaks.com/topic/20470-datetime-problem/ Share on other sites More sharing options...
kenrbnsn Posted September 12, 2006 Share Posted September 12, 2006 Whatever you're passing into your function isn't being recognized as at date/time string by the strtotime() function.You can change your writeTime function to:[code]<?phpfunction writeTime($formatted){return(date('Y-m-d H:i:s',strtotime($formatted)));}?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/20470-datetime-problem/#findComment-90218 Share on other sites More sharing options...
warpdesign Posted September 12, 2006 Author Share Posted September 12, 2006 Thanks for the advice on writing the function more efficient! I'm not a programmer so I know my code is always probably the least efficient way of doing it. When I echo the string that is being passed to the function it is:"Sep/18/2006 08:00 PM" Shouldn't it recognize that in strtotime() ? Basically when I call the function it looks like this:[code]<?phpfunction writeTime($formatted){return(date('Y-m-d H:i:s',strtotime($formatted)));}writeTime($_POST['date']);echo $_POST['date']; // <--- returns Sep/18/2006 08:00 PM?>[/code]I should maybe also mention that I used this .js so the user can pick the date:http://www.rainforestnet.com/datetimepicker.htmSo that is where the input is coming from. Link to comment https://forums.phpfreaks.com/topic/20470-datetime-problem/#findComment-90521 Share on other sites More sharing options...
paul2463 Posted September 12, 2006 Share Posted September 12, 2006 the type of input strtotime() is looking for is[quote]18 Sep 2006 20:00:00[/quote]as per the <a href="http://uk.php.net/strtotime"> Manual </a> Link to comment https://forums.phpfreaks.com/topic/20470-datetime-problem/#findComment-90619 Share on other sites More sharing options...
obsidian Posted September 12, 2006 Share Posted September 12, 2006 [quote author=paul2463 link=topic=107734.msg432962#msg432962 date=1158088391]the type of input strtotime() is looking for is[quote]18 Sep 2006 20:00:00[/quote]as per the <a href="http://uk.php.net/strtotime"> Manual </a> [/quote]actually, per the manual, you can use almost any readable format for strtotime()... any of the following will work:2006-09-18 20:00:009/18/2006 20:00:0018 Sep 2006 20:00:00September 18, 2006 20:00:009/18/2006 8:00PM Link to comment https://forums.phpfreaks.com/topic/20470-datetime-problem/#findComment-90637 Share on other sites More sharing options...
paul2463 Posted September 12, 2006 Share Posted September 12, 2006 cheers for that Obsidian, I stand corrected Link to comment https://forums.phpfreaks.com/topic/20470-datetime-problem/#findComment-90645 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.