Home » Tutorial

Changing Date Format in PHP

Written By: techieDan on April 3, 2009 4 Comments

I have been wondering how I can change my date Format in PHP. Seemingly as I have been very familiar with JAVA and JSP all thru my working life, I thought moving into php would be easy. So then I got myself involved into a simple assignment which I came up myself.

PHP Logo

Though searching for PHP help sometimes is a bit pain in the ass as I got to learn it all once again especially the syntax. One great function is the explode() function, but now though, I had problem displaying the date in the right function.

Unlike Java, I can use the SimpleDateFormat class to change my date into my desired format but not in php.

SimpleDateFormat dtFormat = new SimpleDateFormat(‘dd-MM-yyyy’);

PHP looks like no such function and there are loads out there using explode() function and recombine them later. There has to be a shorter way.

I finally found it and here’s the solution to it here instead.
Let’s say if you retrieve the date data from mySQL it will be like 2009-04-03 which is  format. Now I would like to make it 03-04-2009. Solution is stated below.

echo date(‘d-m-Y’,strtotime(dateVariable));

By using echo I can print out the date in that desired format. Looks like I have a long way to go in my PHP research and play.

Related Posts with Thumbnails

Tags: ,

Digg this!Add to del.icio.us!Stumble this!Add to Techorati!Share on Facebook!Seed Newsvine!Reddit!Add to Yahoo!

4 Responses to “Changing Date Format in PHP”

  1. Lasker says on: 3 April 2009 at 10:04 pm

    Hmm .. for comparison sake, in .NET the code would be:

    date.parse (“dd-mm-yyyy”)

  2. abhishek says on: 17 June 2009 at 2:08 pm

    hey, its very helpful article. nice post.

  3. Anne says on: 23 July 2009 at 11:52 am

    Thanks so much!

  4. billyxs says on: 29 July 2009 at 6:50 am

    Thanks so much for this.

Leave a Reply:

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  Copyright © 2009 TechieDan, All rights reserved.| Powered by WordPress