125

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.

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
Daniel Mabinko
  • 1,351
  • 3
  • 11
  • 10

5 Answers5

332

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));
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
  • 2
    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 '13 at 05:03
  • 34
    @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: http://www.php.net/manual/en/function.strtotime.php – hjpotter92 Apr 24 '13 at 06:01
  • 3
    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 '13 at 05:19
  • 4
    `$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 '15 at 09:57
  • 6
    How can strtotime() differ between mm-dd-yyyy and dd-mm-yyyy – Bakly Sep 23 '16 at 23:59
  • Using / 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. – mattpr May 29 '17 at 08:34
  • I am Trying With the DATE datatype and it's not working, any suggestion. @hjpotter92 – always-a-learner May 30 '17 at 11:19
  • the second method prevents me from extra codes in API part. – Abdulla Nilam Dec 13 '17 at 08:21
  • I 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]; – Mekey Salaria Mar 07 '19 at 11:09
  • Thanks, Works perfectly. Once again thanks for the reason of that `PHP doesn't work well with 'dd/mm/YYYY' format` – Prasad Patel Nov 14 '19 at 13:49
  • This 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). – user1298923 Jul 27 '21 at 15:50
124

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
CreativeMinds
  • 333
  • 3
  • 22
Baba
  • 94,024
  • 28
  • 166
  • 217
  • 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 '18 at 09:15
  • This is the most flexible solution to all date problems. How could i miss this for all the years. Thanks! – Arne Oct 16 '19 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 '22 at 03:46
40

Here's another solution not using date(). not so smart:)

$var = '20/04/2012';
echo implode("-", array_reverse(explode("/", $var)));
tosin
  • 1,159
  • 7
  • 14
  • 7
    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 '13 at 05:21
  • @tosin Very creative solution! – Han Arantes Mar 16 '16 at 14:22
  • 1
    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 '16 at 16:02
  • 1
    @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 07 '19 at 17:36
  • @agaggi That is only if you assume 100% of people reading this solution has the same situation as the OP! – Shadoweb Feb 21 '19 at 16:06
  • Is this method advisable? What is the disadvantage in doing so ? – Vagabond Mar 10 '21 at 15:20
  • 1
    @Vagabond I consider it perfectly advisable, it seems to have no performance or functionality drawbacks for me at least. – Syed M. Sannan Oct 10 '22 at 20:04
6

Do this:

date('Y-m-d', strtotime('dd/mm/yyyy'));

But make sure 'dd/mm/yyyy' is the actual date.

honyovk
  • 2,717
  • 18
  • 26
  • 1
    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: http://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 '15 at 13:52
1

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.

Achraf Almouloudi
  • 756
  • 10
  • 27
  • 2
    @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 08 '14 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 08 '14 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 '14 at 05: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 '14 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 '14 at 18:44
  • Someone just downvoted this answer minutes before I published my improved edit. – Achraf Almouloudi Mar 26 '19 at 22:10