strtotime() Struggles with dd/mm/YYYY Format
When working with thestrtotime()function, it's important to be aware of its date format limitations. While it supports a range of formats, it falters when dealing with the dd/mm/YYYY format.
Solution: Convert dd/mm/YYYY to YYYY-mm-dd
To resolve this issue and convert a date in dd/mm/YYYY format to YYYY-mm-dd, consider the following simplified method:
$date = '25/05/2010'; $date = str_replace('/', '-', $date); echo date('Y-m-d', strtotime($date));
Output:
2010-05-25
strtotime() Documentation
According to thestrtotime()documentation, dates in the m/d/y or d-m-y formats are interpreted based on the separator used. If the separator is a slash (/), the American m/d/y format is assumed. If the separator is a dash (-) or a dot (.), the European d-m-y format is assumed.
In our case, since dd/mm/YYYY uses a dash as a separator, it does not conform to either supported format. Hence, the conversion step using str_replace() is required to transform the date into the accepted d-m-y format before using strtotime().
The above is the detailed content of Why Does `strtotime()` Fail with dd/mm/YYYY and How Can I Fix It?. For more information, please follow other related articles on the PHP Chinese website!