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

匿名 (未验证) 提交于 2019-12-03 01:29:01

问题:

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.

回答1:

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.



回答2:

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); 


回答3:

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.



回答4:

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);  } 


回答5:

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



回答6:

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.



回答7:

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!



回答8:

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.



回答9:

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)



回答10:

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.



回答11:

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



回答12:

The simplest solution is this:

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


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!