48

I have the following date: 2010-04-19 18:31:27. I would like to convert this date to the dd/mm/yyyy format.

7 Answers 7

112

You can use a regular expression or manually manipulate the string, but I prefer:

date("d/m/Y", strtotime($str)); 
Sign up to request clarification or add additional context in comments.

5 Comments

I find on a lot of our servers (operating on Australian time) I need to use: date("d/m/o", strtotime($str)); -> i.e 'o' for the year.
Hi there! I have already tried your solution but does not seems to work. Like I asked on my post, I just need the date in the dd/mm/yyyy format without the time component. Thanks for the help, Best regards!
Which part doesn't work? strtotime("2010-04-19 18:31:27"); returns the seconds since the epoch, 1271716287 (for my TZ). date() takes a format string and an epoch int; the format string tells it what to return, and "d/m/Y" is days as a zero-padded int, months as a zero-padded int, and year as four digits. Since no time parameters are included in the format string (e.g. "d/m/Y h:i:s"), they won't be returned
Hi there! I was commiting an error with the application of the suggested solution. It worked just fine! To all others, thanks for the responses. Thanks again for the response! Best regards
And current date date("d/m/Y");
10
<?php $test1='2010-04-19 18:31:27'; echo date('d/m/Y',strtotime($test1)); ?> 

try this

Comments

3

If your date is in the format of a string use the explode function

 array explode ( string $delimiter , string $string [, int $limit ] ) //In the case of your code $length = strrpos($oldDate," "); $newDate = explode( "-" , substr($oldDate,$length)); $output = $newDate[2]."/".$newDate[1]."/".$newDate[0]; 

Hope the above works now

2 Comments

This is far more complex of a solution than what is needed.
But useful for other possible cases.
2

There is also the DateTime object if you want to go that way: http://www.php.net/manual/en/datetime.construct.php

Comments

1

Simply Try $date=date('d/m/Y',strtotime($yourdatevariable))

or else you want to convert to d-m-Y Just Simply Edit above d/m/Y with d-m-Y

Comments

-1

Try this:

$old_date = Date_create("2010-04-19 18:31:27"); $new_date = Date_format($old_date, "d/m/Y"); 

Comments

-4
$source = 'your varible name'; $date = new DateTime($source); $_REQUEST["date"] = $date->format('d-m-Y'); echo $_REQUEST["date"]; 

2 Comments

Please don't leave signatures under your post.
The question says convert to "dd/mm/yyyy". This solution seems to give the wrong format.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.