Strtotime() doesn't work with dd/mm/YYYY format

Php

Php Problem Overview


I really like the strtotime() function, but the user manual doesn't give a complete description of the supported date formats. strtotime('dd/mm/YYYY') doesn't work, it works only with mm/dd/YYYY format.

If I have date in dd/mm/YYYY format, how can I convert it to YYYY-mm-dd? I can do it by using explode() function, but I think there are better solutions.

Php Solutions


Solution 1 - Php

Here is the simplified solution:

$date = '25/05/2010';
$date = str_replace('/', '-', $date);
echo date('Y-m-d', strtotime($date));

Result:

2010-05-25

The strtotime documentation reads:

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

Solution 2 - Php

You can parse dates from a custom format (as of PHP 5.3) with DateTime::createFromFormat

$timestamp = DateTime::createFromFormat('!d/m/Y', '23/05/2010')->getTimestamp();

(Aside: The ! is used to reset non-specified values to the Unix timestamp, ie. the time will be midnight.)


If you do not want to (or cannot) use PHP 5.3, then a full list of available date/time formats which strtotime accepts is listed on the Date Formats manual page. That page more thoroughly describes the fact that m/d/Y is inferred over d/m/Y (but you can, as mentioned in the answers here, use d-m-Y, d.m.Y or d\tm\tY).


In the past, I've also resorted to the quicky str_replace mentioned in another answer, as well as self-parsing the date string into another format like

$subject   = '23/05/2010';
$formatted = vsprintf('%3$04d/%2$02d/%1$02d', sscanf($subject,'%02d/%02d/%04d'));
$timestamp = strtotime($formatted);

Solution 3 - Php

This is a good solution to many problems:

function datetotime ($date, $format = 'YYYY-MM-DD') {

	if ($format == 'YYYY-MM-DD') list($year, $month, $day) = explode('-', $date);
	if ($format == 'YYYY/MM/DD') list($year, $month, $day) = explode('/', $date);
	if ($format == 'YYYY.MM.DD') list($year, $month, $day) = explode('.', $date);
	
	if ($format == 'DD-MM-YYYY') list($day, $month, $year) = explode('-', $date);
	if ($format == 'DD/MM/YYYY') list($day, $month, $year) = explode('/', $date);
	if ($format == 'DD.MM.YYYY') list($day, $month, $year) = explode('.', $date);
	
	if ($format == 'MM-DD-YYYY') list($month, $day, $year) = explode('-', $date);
	if ($format == 'MM/DD/YYYY') list($month, $day, $year) = explode('/', $date);
	if ($format == 'MM.DD.YYYY') list($month, $day, $year) = explode('.', $date);
	
	return mktime(0, 0, 0, $month, $day, $year);

}

Solution 4 - Php

From the STRTOTIME writeup 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.

It is as simple as that.

Solution 5 - Php

$srchDate = date_format(date_create_from_format('d/m/Y', $srchDate), 'Y/m/d');

This will work for you. You convert the String into a custom date format where you can specify to PHP what the original format of the String is that had been given to it. Now that it is a date format, you can convert it to PHP's default date format, which is the same that is used by MySQL.

Solution 6 - Php

fastest should probably be

false!== ($date !== $date=preg_replace(';[0-2]{2}/[0-2]{2}/[0-2]{2};','$3-$2-$1',$date))

this will return false if the format does not look like the proper one, but it wont-check wether the date is valid

Solution 7 - Php

I tried to convert ddmmyy format to date("Y-m-d" format but was not working when I directly pass ddmmyy =date('dmy')

then realized it has to be in yyyy-mm-dd format so. used substring to organize

$strParam = '20'.substr($_GET['date'], 4, 2).'-'.substr($_GET['date'], 2, 2).'-'.substr($_GET['date'], 0, 2);  

then passed to date("Y-m-d",strtotime($strParam));

it worked!

Solution 8 - Php

I haven't found a better solution. You can use explode(), preg_match_all(), etc.

I have a static helper function like this

class Date {

    public static function ausStrToTime($str) {
        $dateTokens = explode('/', $str);
        return strtotime($dateTokens[1] . '/' . $dateTokens[0] . '/' . $dateTokens[2]); 

    }

}

There is probably a better name for that, but I use ausStrToTime() because it works with Australian dates (which I often deal with, being an Australian). A better name would probably be the standardised name, but I'm not sure what that is.

Solution 9 - Php

Are you getting this value from a database? If so, consider formatting it in the database (use date_format in mysql, for example). If not, exploding the value may be the best bet, since strtotime just doesn't seem to appreciate dd/mm/yyyy values.

Solution 10 - Php

If you know it's in dd/mm/YYYY, you can do:

    $regex = '#([/d]{1,2})/([/d]{1,2})/([/d]{2,4})#';
    $match = array();
    if (preg_match($regex, $date, $match)) {
        if (strlen($match[3]) == 2) {
            $match[3] = '20' . $match[3];
        }
        return mktime(0, 0, 0, $match[2], $match[1], $match[3]);
    }
    return strtotime($date);

It will match dates in the form d/m/YY or dd/mm/YYYY (or any combination of the two)...

If you want to support more separators than just /, you can change the regex to:

    $regex = '#([\d]{1,2})[/-]([\d]{1,2})[/-]([\d]{2,4})#';

And then add any characters you want into the [/-] bit (Note, the - character needs to be last)

Solution 11 - Php

This workaround is simpler and more elegant than explode:

$my_date = str_replace("/", ".", $my_date);
$my_date = strtotime($my_date);
$my_date = date("Y-m-d", $my_date);

You don't have to know what format you're getting the date in, but if it comes with slashes they are replaced with full stops and it is treated as European by strtotime.

Solution 12 - Php

Simple as this

date('Y-m-d H:i:s', strtotime( str_replace('/', '-', $from_date ) ) );

Solution 13 - Php

The simplest solution is this:

$date    = '07/28/2010';
$newdate = date('Y-m-d', strtotime($date));

Solution 14 - Php

This the solution i'm using even if the leading zero are there.

<?php
 $date = '05/05/2017';
 $date = str_replace('/', '-', $date);
 $date = date('Y-m-d', strtotime($date));

 echo $date;
?>

Solution 15 - Php

$date_info = '20/02/2019'; echo date('Y-m-d', strtotime(str_replace('/', '-', $date_info) ));

Solution 16 - Php

If nothing worked, try this.

$date = '25/05/2010'; //dd/mm/YYYY
$dateAr = explode('/',);
$date = $dateAr[2].'-'.$dateAr[1].'-'.$dateAr[0]; //YYYY-mm-dd

Solution 17 - Php

{{ date('d F Y',strtotime($a->dates)) }}

alternative use laravel

\Carbon\Carbon::parse($a->dates)->format('d F Y') }}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSimonView Question on Stackoverflow
Solution 1 - PhpWeb LogicView Answer on Stackoverflow
Solution 2 - PhpsalatheView Answer on Stackoverflow
Solution 3 - PhpJonathan RoyView Answer on Stackoverflow
Solution 4 - PhpL. SmitView Answer on Stackoverflow
Solution 5 - PhpFi HoranView Answer on Stackoverflow
Solution 6 - PhpskullnobrainsView Answer on Stackoverflow
Solution 7 - Phpsmakintel.comView Answer on Stackoverflow
Solution 8 - PhpalexView Answer on Stackoverflow
Solution 9 - PhpJoe MasteyView Answer on Stackoverflow
Solution 10 - PhpircmaxellView Answer on Stackoverflow
Solution 11 - Phpuser2623027View Answer on Stackoverflow
Solution 12 - PhpPrajwol KCView Answer on Stackoverflow
Solution 13 - Phpsimply_jhenView Answer on Stackoverflow
Solution 14 - PhpNandhiniView Answer on Stackoverflow
Solution 15 - PhpNazmul HaqueView Answer on Stackoverflow
Solution 16 - PhpDisapamokView Answer on Stackoverflow
Solution 17 - PhpDoYouOneView Answer on Stackoverflow