Jump to content

Archived

This topic is now archived and is closed to further replies.

warpdesign

date/time problem

Recommended Posts

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?? ???

Share this post


Link to post
Share on other sites
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]<?php
function writeTime($formatted){
return(date('Y-m-d H:i:s',strtotime($formatted)));
}?>[/code]

Ken

Share this post


Link to post
Share on other sites
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]
<?php
function 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.htm

So that is where the input is coming from.

Share this post


Link to post
Share on other sites
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>

Share this post


Link to post
Share on other sites
[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:00
9/18/2006 20:00:00
18 Sep 2006 20:00:00
September 18, 2006 20:00:00
9/18/2006 8:00PM

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.