Pretty Dates from MySQL DateTime

So I'm working on a project where I want to show a formatted join date on a user profile page.  The join date, of course, is stored in a MySQL database using the datetime column type.  I know you can use the MySQL built in function DATE_FORMAT, but I wanted to be able to do this right in my PHP rather than in a query.

So here's an easy, one-line bit of code to give you a date in any format you choose using your datetime value from your database result:

PHP:
  1. $j = $result['datetimefield'];
  2. //this formatted date displays a Month Name and Year
  3. $formatted_date = date('F Y', mktime(substr($j, 11, 2), substr($j, 14, 2), substr($j, 17, 2), substr($j, 5, 2), substr($j, 8, 2), substr($j, 0, 4)));

Is this helpful for you?  Am I trying to roll a square wheel here and you know a better way?  Comment and let me know!

Tags: , , ,

This entry was posted on Friday, June 13th, 2008.
You can leave a comment, or trackback from your own site.

No Comments Yet

You can be the first to comment!

Leave a comment