Twitter
RSS

Friendly MySQL Dates with PHP

October 22, 2008 - 2 Comments Bookmark and Share

With so many of programmers and designers coming across database oriented job opportunities, I thought I’d point out one small coding trick when it comes to handling MySQL dates with PHP. This article assumes that you have had some experience with PHP, though this code shouldn’t be too hard for beginners.

Step 1 – Determine Your Date Format

When it comes to outputting a date, PHP is very dynamic. You can make PHP output just the current month, or today’s date, or an exact time-stamp down to the second. Here are some examples of the ways the PHP date() function can be used:

<?php
// Assuming today is: March 10th, 2001, 5:16:18 pm
$today = date('F j, Y, g:i a'); will print March 10, 2001, 5:16 pm
$today = date('m.d.y'); will print 03.10.01
$today = date('Ymd'); will print 20010310
$today = date('h-i-s, j-m-y '); will print 05-16-17, 10-03-01
?>

So, the first step in converting you date-stamp into a readable format is to choose how you’d like your date to appear on your web page. For this tutorial, I will be converting my dates into the date(‘F j, Y’) format so my dates look like March 10, 2001.

Step 2 – The Problem

The problem that many programmers run into is that the default format for a MySQL date-stamp is date(‘Y-m-d’), which outputs the year, then month, then day (2008-05-06). This time format is very unfriendly to read, so I’m going to show you a quick way to convert MySQL’s default date-stamp into a friendly, readable format.

Step 3 – The Code

So lets build the code for this project. Using PHP’s strtotime() we can make out MySQL date readable again.

<?php
$timestamp = strtotime('1981-07-04');
$newdate = date('F j, Y', $timestamp);
echo $newdate;
?>

Our time transformation code begins with the creation of our own variable. So, in line two we are setting the $timestamp variable equal to our date-stamp, but we are transforming the date-stamp with PHP’s strtotime() and date() functions.

The strtotime() function expects to be given a variable containing a US English date format and will try to parse that format into a Unix timestamp. We are feeding a default styled MySQL date-stamp to the strtotime() function in order for PHP’s date function to take that strtotime() UNIX date and convert it to a readable format. Did you get that? In short, the strtotime() changes our date into a UNIX date format, then the date() function, in line three of our code, transforms that new UNIX format date into a readable format.

Now that we can transform MySQL styled dates into a readable format, all you have to do is replace the date in our script (’1981-07-04′) with the MySQL date from the database. The variable would then look something like:

$timestamp = strtotime($yourdatestamp);

Finished!

That’s it. You can now transform your MySQL dates on that next big project. Happy coding!

Comments

  1. mysql format date(January 9, 2010)

    Here is a useful website for formatting dates using the date_format function

  2. fayaz(April 25, 2010)

    Hmm nice Function and

    nice tutorials of time…

    Thnx

Leave a Reply