Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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 to explode 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.

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 .

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));
                Here get only 1970-01-01 if i am using dd/mm/yyyy format.  mm/dd/yyyy format is ok with this code.
– Sibiraj PR
                Apr 24, 2013 at 5:03
                @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
– hjpotter92
                Apr 24, 2013 at 6:01
                This 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
                Dec 18, 2013 at 5:19
                $var = '20/04/2012'; $date = str_replace('/', '-', $var); echo date('Y-m-d', strtotime($date)); // This solution works for me for dd/mm/yy
– Linga
                Dec 16, 2015 at 9:57

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
                when 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"
– Jayani Sumudini
                Mar 25, 2018 at 9:15
                This is the most flexible solution to all date problems. How could i miss this for all the years. Thanks!
– Arne
                Oct 16, 2019 at 12:11
                still 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
– Vilthering
                Jun 22, 2022 at 3:46
                If 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.
– Tim Ogilvy
                Dec 18, 2013 at 5:21
                That would work only if they add the 0 like 05/04/2010, if they write it 5/4/2010 it won't work.
– Shadoweb
                Oct 18, 2016 at 16:02
                @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.
– Dr. DS
                Feb 7, 2019 at 17:36
                @Vagabond I consider it perfectly advisable, it seems to have no performance or functionality drawbacks for me at least.
– Stranger
                Oct 10, 2022 at 20:04
                This is problematic as strtotime will read a date with the / separator as American, i.e. m/d/Y whereas - as European d-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.
– haakym
                Apr 20, 2015 at 13:52

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.

@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. – Achraf Almouloudi Jul 8, 2014 at 18:07 I got through some study and see your point. You're right. Although you did not answer the question. – Vitor Tyburski Jul 8, 2014 at 19:55 @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. – Achraf Almouloudi Jul 10, 2014 at 5:32 I 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. – Vitor Tyburski Jul 10, 2014 at 10:46 I have also edited my reply to reflect the fact that it's just a note, not exactly a reply to the asked question. – Achraf Almouloudi Jul 11, 2014 at 18:44