PHP convert date format dd/mm/yyyy => yyyy-mm-dd
Solution 1
Dates in the
m/d/y
ord-m-y
formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/
), then the Americanm/d/y
is assumed; whereas if the separator is a dash (-
) or a dot (.
), then the Europeand-m-y
format is assumed. Check more here.
Use the default date function.
$var = "20/04/2012";
echo date("Y-m-d", strtotime($var) );
EDIT I just tested it, and somehow, PHP doesn't work well with dd/mm/yyyy format. Here's another solution.
$var = '20/04/2012';
$date = str_replace('/', '-', $var);
echo date('Y-m-d', strtotime($date));
Solution 2
Try Using DateTime::createFromFormat
$date = DateTime::createFromFormat('d/m/Y', "24/04/2012");
echo $date->format('Y-m-d');
Output
2012-04-24
EDIT:
If the date is 5/4/2010 (both D/M/YYYY or DD/MM/YYYY), this below method is used to convert 5/4/2010 to 2010-4-5 (both YYYY-MM-DD or YYYY-M-D) format.
$old_date = explode('/', '5/4/2010');
$new_data = $old_date[2].'-'.$old_date[1].'-'.$old_date[0];
OUTPUT:
2010-4-5
Solution 3
Here's another solution not using date(). not so smart:)
$var = '20/04/2012';
echo implode("-", array_reverse(explode("/", $var)));
Solution 4
Do this:
date('Y-m-d', strtotime('dd/mm/yyyy'));
But make sure 'dd/mm/yyyy' is the actual date.
Solution 5
I can see great answers, so there's no need to repeat here, so I'd like to offer some advice:
I would recommend using a Unix Timestamp integer instead of a human-readable date format to handle time internally, then use PHP's date()
function to convert the timestamp value into a human-readable date format for user display. Here's a crude example of how it should be done:
// Get unix timestamp in seconds
$current_time = date();
// Or if you need millisecond precision
// Get unix timestamp in milliseconds
$current_time = microtime(true);
Then use $current_time
as needed in your app (store, add or subtract, etc), then when you need to display the date value it to your users, you can use date()
to specify your desired date format:
// Display a human-readable date format
echo date('d-m-Y', $current_time);
This way you'll avoid much headache dealing with date formats, conversions and timezones, as your dates will be in a standardized format (Unix Timestamp) that is compact, timezone-independent (always in UTC) and widely supported in programming languages and databases.
Related videos on Youtube
Daniel Mabinko
Updated on February 03, 2020Comments
-
Daniel Mabinko about 4 years
I am trying to convert a date from
dd/mm/yyyy => yyyy-mm-dd
. I have using the mktime() function and other functions but I cannot seem to make it work. I have managed toexplode
the original date using'/'
as the delimiter but I have no success changing the format and swapping the'/'
with a'-'
.Any help will be greatly appreciated.
-
Adeel Ahmed Baloch about 4 yearsTry this one $old_date = "20/04/2012"; $new_date = date("Y-m-d", strtotime($old_date) );
-
-
Sibiraj PR about 11 yearsHere get only 1970-01-01 if i am using dd/mm/yyyy format. mm/dd/yyyy format is ok with this code.
-
hjpotter92 about 11 years@SibirajPR Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. Check more here: php.net/manual/en/function.strtotime.php
-
Sibiraj PR about 11 yearsThank you for your valuable information.
-
Tim Ogilvy over 10 yearsThis looks okay but isn't. 05/04/2045 will assume American and give the 4th of May instead of the 4th of April. It's an intermittant and dangerous mistake - as per hjpotter. This answer should be marked as wrong.
-
Tim Ogilvy over 10 yearsIf it's not smart, why is it so clever. I can't see any problems with this solution at all, and it saves screwing around with dates and localities. Guaranteed performance every time.
-
Achraf Almouloudi almost 10 years@VitorTyburski You can't be serious? Any professional programmer with his right mind knows that storing the date as a Timestamp is the best way to deal with date formatting and adding or removing bits of time, because all it takes is to add or subtract a number of seconds to the timestamp value.
-
Vitor Tyburski almost 10 yearsI got through some study and see your point. You're right. Although you did not answer the question.
-
Achraf Almouloudi almost 10 years@VitorTyburski Why did you remove your first comment then? It just shows that you're relying too much on the "delete" feature to hide your mistakes. I haven't answered the question because by the time I was on the thread, a best answer was already there, so I just wanted to make a point that using a timestamp value is good practice.
-
Vitor Tyburski almost 10 yearsI removed the first comment wrongly, but if you wish I state it again: "Don't use timestamps, as they can lead to problems with DST, etc...". Again my comment IS WRONG, and I apologize. I simply don't remove the downvote because, I can't AND, most importantly, because you should have done a comment. If you edit your answer I will gladly remove the downvote. Sorry about that.
-
Achraf Almouloudi almost 10 yearsI have also edited my reply to reflect the fact that it's just a note, not exactly a reply to the asked question.
-
haakym about 9 yearsThis is problematic as strtotime will read a date with the
/
separator as American, i.e.m/d/Y
whereas-
as Europeand-m-Y
. See the accepted answer's edit and php docs: php.net/manual/en/function.strtotime.php - Note: Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. -
Linga over 8 years
$var = '20/04/2012'; $date = str_replace('/', '-', $var); echo date('Y-m-d', strtotime($date));
// This solution works for me for dd/mm/yy -
Han Arantes about 8 years@tosin Very creative solution!
-
Bakly over 7 yearsHow can strtotime() differ between mm-dd-yyyy and dd-mm-yyyy
-
Shadoweb over 7 yearsThat would work only if they add the 0 like 05/04/2010, if they write it 5/4/2010 it won't work.
-
mattpr almost 7 yearsUsing / to assume american format (MM/DD/YY) is dangerous because in the UK they often use / but follow the european convention (smallest to largest...DD/MM/YY.
-
ankit suthar almost 7 yearsI am Trying With the DATE datatype and it's not working, any suggestion. @hjpotter92
-
Abdulla Nilam over 6 yearsthe second method prevents me from extra codes in API part.
-
Jayani Sumudini about 6 yearswhen you have mm/dd/yyyy format and want to convert yyyy-mm-dd format,then try this.This worked for me. $var1 = "03/01/2014"; $date = DateTime::createFromFormat('m/d/Y', $var1); $travel_date = $date->format('Y-m-d'); output = "2014-03-01"
-
Dr. DS about 5 years@Shadowbob, OP asked for dd/mm/yyyy conversion, so 5/4/2010 could never be the input. dd and mm naming always have 0 preceding in case of one digit date or month. This is smart solution.
-
Shadoweb about 5 years@agaggi That is only if you assume 100% of people reading this solution has the same situation as the OP!
-
Mekey Salaria about 5 yearsI used the following code although its not the complete and better solution, but just a hack $var = $crow['date']; $ex = explode('/',$var); $dd = $ex[2].'-'.$ex[1].'-'.$ex[0];
-
Achraf Almouloudi about 5 yearsSomeone just downvoted this answer minutes before I published my improved edit.
-
Arne over 4 yearsThis is the most flexible solution to all date problems. How could i miss this for all the years. Thanks!
-
Prasad Patel over 4 yearsThanks, Works perfectly. Once again thanks for the reason of that
PHP doesn't work well with 'dd/mm/YYYY' format
-
Yash Karanke over 4 yearsthis really helped me thank you!
-
Plugie about 3 yearsThis should be the correct answer
-
Vagabond about 3 yearsIs this method advisable? What is the disadvantage in doing so ?
-
Sobir almost 3 yearsIt worked for me thanks bro :)
-
user1298923 almost 3 yearsThis fails in cases where the date format is 'm/d/Y', resulting in a conversion to '1970-01-01'. This 'm/d/Y' date format is seen while handling Excel files in the pt-PT language. Was able to convert correctly in all cases via Baba's answer below, using DateTime::createFromFormat('m/d/Y', $date).
-
Vilthering almost 2 yearsstill not correct, when u put date $date = DateTime::createFromFormat('d/m/Y', "06/16/2022"); echo $date->format('Y-m-d'); the result will be : 2023-04-06