Random Snippets

  • Home
  • Sequence analysis blog
  • Categories
    • javascript
    • mysql
    • Uncategorized
  • Subscribe via RSS

How to convert MySQL timestamp to PHP date type

October 5th, 2008  |  Published in mysql  |  2 Comments

Let’s say you have the following PHP code that extracts the date from the times table in your MySQL database. The date is of timestamp type which has the following format: ‘YYYY-MM-DD HH:MM:SS’ or ‘2008-10-05 21:34:02.’

1
2
3
4
$res = mysql_query("SELECT date FROM times;");
while ( $row = mysql_fetch_array($res) ) {
   echo $row['date'] . "<br />";
}


This date format that is in the output is in the timestamp format, 2008-10-05 21:34:02, which is not surprising, but you want something that is more “user-friendly” or “readable” as in “9:34 pm October 5, 2008.”

Let’s go back to the drawing board and try again:

1
2
3
4
$res = mysql_query("SELECT date FROM times;");
while ( $row = mysql_fetch_array($res) ) {
   echo date("g:i a F j, Y ", strtotime($row["date"])) . "<br />";
}

A sample output from this PHP code would be 9:34 pm October 5, 2008 which is much more user-friendly.

The PHP strtotime function parses the MySQL timestamp into a Unix timestamp which can be utilized for further parsing or formatting in the PHP date function.

Here are some other sample date output formats that may be of practical use:

1
2
3
4
5
6
echo date("F j, Y g:i a", strtotime($row["date"]));                  // October 5, 2008 9:34 pm
echo date("m.d.y", strtotime($row["date"]));                         // 10.05.08
echo date("j, n, Y", strtotime($row["date"]));                       // 5, 10, 2008
echo date("Ymd", strtotime($row["date"]));                           // 20081005
echo date('\i\t \i\s \t\h\e jS \d\a\y.', strtotime($row["date"]));   // It is the 5th day.
echo date("D M j G:i:s T Y", strtotime($row["date"]));               // Sun Oct 5 21:34:02 PST 2008
Share with a friend:
    

Customize message


[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Responses

Feed Trackback Address
  1. Chris says:

    November 6th, 2008 at 1:14 pm (#)

    Great, exactly what i was looking for!
    Saved me a lot of time!

    Thanks

    [Reply]

  2. Danny says:

    May 23rd, 2009 at 7:22 am (#)

    Thanks for the clear and nicely written explanation.
    My next adventure is to try and figure out how to calculate time difference between this result and now.
    I want to display strings like “2 hours ago”, “1 week ago” etc.

    Thanks again.

    [Reply]

Comments or feedback...

If you have any demos that you would like to request, please do so.

Click to cancel reply

Recent Posts

  • The dangers of embedding the notorious “void(0)” JavaScript code in the href attribute of the “a” tag
  • How to randomly order or select rows in a MySQL query
  • How to convert MySQL timestamp to PHP date type
  • How to count values with MySQL queries
  • How to confirm or prompt user for input via JavaScript

Recent Comments

  • Ulysses on How to hide, show, or toggle your div
  • Steve Caldwell on How to dynamically add form elements via JavaScript
  • minie on How to dynamically add form elements via JavaScript
  • Sub on How to hide, show, or toggle your div
  • Pieter on How to hide, show, or toggle your div

Archives

Tag Cloud

appendchild checkboxes collapse color background demo demo show div element div id document object model dynamic Dynamically dynamicContent dynamic text email form error warnings form form email getElementById Hide html javascript input button input text javascript javascript code javascript function javascript functions javascript onclick event loop through menu mysql mysql query onClick onclick event parsing query regex remove removeChild removeElement size font styling text element text input text inputs verification

©2009 Random Snippets