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
You can use a regular expression or manually manipulate the string, but I prefer:
date("d/m/Y", strtotime($str)); 5 Comments
niggles
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.
Rui Gonçalves
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!
Michael Mrozek
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 returnedRui Gonçalves
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
Junior Mayhe
And current date
date("d/m/Y");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
Mitch Dempsey
This is far more complex of a solution than what is needed.
James P.
But useful for other possible cases.
There is also the DateTime object if you want to go that way: http://www.php.net/manual/en/datetime.construct.php
Comments
$source = 'your varible name'; $date = new DateTime($source); $_REQUEST["date"] = $date->format('d-m-Y'); echo $_REQUEST["date"]; 2 Comments
Glorfindel
Please don't leave signatures under your post.
Pang
The question says convert to "dd/mm/yyyy". This solution seems to give the wrong format.