Jump to content


Photo

datetime mysql to php solution


  • Please log in to reply
2 replies to this topic

#1 tecdesign

tecdesign
  • Members
  • PipPipPip
  • Advanced Member
  • 33 posts

Posted 16 June 2006 - 10:56 AM

If you want to take a timestamp from a mysql database you must convert it into a unix timestamp to echo/print out a correct date.

$result=mysql_query ("SELECT UNIX_TIMESTAMP(mysql_column) as epoch_time FROM table"); 

$unix_timestamp = mysql_result ($result, 0, 0); 
                           echo date("F j, Y @ g:ia", $unix_timestamp);

NB: This is an add on from the tutorial given here in the new snippet section. I just tried this code out and it works and it will solve alot of peoples problems, feel free to add it in as an extension. The as epoch_time is not neccessary for this situation as it is only renaming the column.

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 16 June 2006 - 04:22 PM

Or you can just use PHPs builtin function strtotime which converts the date into a unix timestamp, like so:
$result=mysql_query ("SELECT time_col FROM table");
$row = mysql_fetch_array($result);

$date = date("F j, Y @ g:ia", strtotime($row['time_col']));

echo $date;


#3 tecdesign

tecdesign
  • Members
  • PipPipPip
  • Advanced Member
  • 33 posts

Posted 16 June 2006 - 09:53 PM

[!--quoteo(post=384636:date=Jun 16 2006, 04:22 AM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ Jun 16 2006, 04:22 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Or you can just use PHPs builtin function strtotime which converts the date into a unix timestamp, like so:
$result=mysql_query ("SELECT time_col FROM table");
$row = mysql_fetch_array($result);

$date = date("F j, Y @ g:ia", strtotime($row['time_col']));

echo $date;
[/quote]

I've tried that and it doesn't seem to work for some reason... As it will just put the default date of 1970 or 1900 if you have php 5. My way seems to be successful in php 4.3.0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users