If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

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: , , ,

Leave a Reply

A little about Vernon...

Hey there everybody, I’m Vernon. I’ve been a full-time freelance web designer since 2002 and can honestly say it’s been a great journey.

If you’re interested, take a look at my services site and let me help you with your project.