My Stuff
Username

Password

Articles/Snippets
ASP Classic
C
C#
C++
CSS
HTML
Java
Javascript
MySQL
Perl
PHP
Python
Ruby
Unix/Linux/BSD
VB 6
VB.NET


Converting MySQL Timestamp to a Unix Timestamp


No votes yet

If you use MySQL and have a field defined as 'timestamp', then you may find problems when trying to format the time when using PHP. This tutorial will instruct you how to convert a MySQL timestamp into a Unix timestamp directly from the query.

For this example we will use a table named "EXAMPLE_TABLE" and a field named "TIME". If you try to query the MySQL database and format the TIME result in PHP, you may find that the date is completely wrong. This is because you must request the TIME field in a Unix Format. Below is an example how to pull the TIME field out of the MySQL database and format it with PHP.

<?
$result=mysql_query("SELECT UNIX_TIMESTAMP(TIME) AS FORMATED_TIME FROM EXAMPLE_TABLE");
$FORMATED_TIME=mysql_result($result,0,"FORMATED_TIME");
 
$date=date("m-d-Y H:i:sa",$FORMATED_TIME);
 
echo "$date";
?>

This will print to the browser something like this:
4-20-2002 6:30:21am

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • You can use BBCode tags in the text.
  • You can enable syntax highlighting of source code with the following tags: [code], [blockcode], [asp], [awk], [c], [cpp], [csharp], [drupal5], [drupal6], [java], [javascript], [jquery], [mysql], [perl], [perl6], [php], [python], [ruby], [sql], [vb], [vbnet], [xml].
  • Use to create page breaks.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.