Reputation: 10410
I have the following timeformat:15/08/2011 12:32:23
day/month/year hour:minute:sec and I want to convert to the following format: Y-m-d H:i:s
I tried with date('Y-m-d H:i:s', strtotime($time))
but it not works. It swaps the month and the day when it's converting from string to datenum.
Upvotes: 0
Views: 4864
Reputation: 1082
Replace slashes with hyphen in the date then try it will work.
$a = '07/08/2019'; // 07 is day 08 is month.
echo date('Y-m-d', strtotime($a)); //output: 2019-07-08 where 07 became month.
$a = str_replace("/","-","07/08/2019"); // 07-08-2019
echo date('Y-m-d', strtotime($a)); //2019-08-07
Upvotes: 0
Reputation: 4449
You are going to have to reparse the time. strtotime is thinking you are trying to input a string in the format of 'm/d/Y H:i:s'
, but you are supplying 'd/m/Y H:i:s'
.
list($date, $times) = explode(' ', $time);
list($day, $month, $year) = explode('/', $date);
$newTime = date('Y-m-d H:i:s', strtotime("$month/$day/$year $times");
Upvotes: 0
Reputation: 9884
strtotime
understands both American (mm/dd/YYYY
) and European (dd-mm-YYYY or dd.mm.YYYY
) formats. You are using slashes to separate day, month and year, and that's why your date is interpreted as American. To solve that, replace the slashes with dashes.
Upvotes: 2
Reputation: 175088
In which case, you could very simply swap the month and date from your string:
$time_string = "15/08/2011 12:32:23";
$strtotime = explode("/",$time_string);
$strtotime = implode("/",array($strtotime[1], $strtotime[0], $strtotime[2]));
echo date('Y-m-d H:i:s', strtotime($strtotime));
Working Example: http://codepad.viper-7.com/234toO
Upvotes: 0