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

前端 未结 15 1447
Happy的楠姐
Happy的楠姐 2020-11-22 09:33

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\')<

15条回答
  •  甜味超标
    2020-11-22 09:57

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

提交回复
热议问题