Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

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

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    php
  » Converting MySQL Timestamp to a Unix Timestamp
      by sde
 Page 1 of 1 
   

(Login to remove green text ads)
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.
PHP Code:
<?
$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:
Code:
4-20-2002 6:30:21am





 
 Page 1 of 1 
   

Rate This Article
1 2 3 4 5 6 7 8 9 10





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle