Jump to content


Photo

date/time problem


  • Please log in to reply
5 replies to this topic

#1 warpdesign

warpdesign
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 12 September 2006 - 05:05 AM

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
function inputTime($timestamp){
$timestamp = strtotime($timestamp);
$formated = date("M/d/Y h:i A", $timestamp);
return $formated;
}
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
function writeTime($timestamp){
$timestamp = strtotime($timestamp);
$formated = date("Y-m-d H:i:s", $timestamp);
return $formated;
}

So the problem is, when I do that all my dates come out as Wed, December 31st 1969, 3:59 PM?? ???

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 12 September 2006 - 05:28 AM

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:
<?php
function writeTime($formatted){
return(date('Y-m-d H:i:s',strtotime($formatted)));
}?>

Ken


#3 warpdesign

warpdesign
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 12 September 2006 - 04:15 PM

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:

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

I should maybe also mention that I used this .js so the user can pick the date:
http://www.rainfores...etimepicker.htm

So that is where the input is coming from.


#4 paul2463

paul2463
  • Members
  • PipPipPip
  • Advanced Member
  • 955 posts
  • LocationUK

Posted 12 September 2006 - 07:13 PM

the type of input strtotime() is looking for is

18 Sep 2006 20:00:00


as per the <a href="http://uk.php.net/strtotime"> Manual </a>
you cannot affect the past but you can ruin a perfectly good present by worrying about the future

*********************************************************************
The <A HREF="http://www.php.net/d...php">MANUAL</A> is actually a useful resource

#5 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 12 September 2006 - 07:42 PM

the type of input strtotime() is looking for is

18 Sep 2006 20:00:00


as per the <a href="http://uk.php.net/strtotime"> Manual </a>


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
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#6 paul2463

paul2463
  • Members
  • PipPipPip
  • Advanced Member
  • 955 posts
  • LocationUK

Posted 12 September 2006 - 07:52 PM

cheers for that Obsidian, I stand corrected
you cannot affect the past but you can ruin a perfectly good present by worrying about the future

*********************************************************************
The <A HREF="http://www.php.net/d...php">MANUAL</A> is actually a useful resource




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users