Jump to content

datetime mysql to php solution


tecdesign

Recommended Posts

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.

[code]$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);
[/code]

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.
Link to comment
https://forums.phpfreaks.com/topic/12142-datetime-mysql-to-php-solution/
Share on other sites

Or you can just use PHPs builtin function strtotime which converts the date into a unix timestamp, like so:
[code]$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;[/code]
[!--quoteo(post=384636:date=Jun 16 2006, 04:22 AM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ Jun 16 2006, 04:22 AM) [snapback]384636[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Or you can just use PHPs builtin function strtotime which converts the date into a unix timestamp, like so:
[code]$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;[/code]
[/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

Archived

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

×
×
  • Create New...

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.