Changing Date Format in PHP

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.

6 Comments

  1. Lasker April 3, 2009
  2. abhishek June 17, 2009
  3. Anne July 23, 2009
  4. billyxs July 29, 2009
  5. hADI August 30, 2010
  6. Rajeshwaran May 30, 2012

Leave a Reply to Lasker Cancel reply