Home >Backend Development >PHP Tutorial >How Can I Efficiently Convert dd/mm/YYYY Dates Using PHP's strtotime()?
strtotime() Conversion of Dates in dd/mm/YYYY Format
The strtotime() function is a powerful tool for converting dates into timestamps. However, the documentation for this function does not fully cover all the supported date formats. Specifically, strtotime() struggles with dates in dd/mm/YYYY format.
To address this issue, a common workaround is to manually explode the date string using the explode() function. However, there is a more efficient solution:
$date = '25/05/2010'; $date = str_replace('/', '-', $date); echo date('Y-m-d', strtotime($date));
This snippet of code converts the date from dd/mm/YYYY to YYYY-mm-dd by replacing the forward slashes (/) with hyphens (-) and then using strtotime() to convert the resulting string into a timestamp. Finally, the date is formatted using the Y-m-d format specifier.
The result is a properly formatted date:
2010-05-25
According to the strtotime() documentation, dates in the formats m/d/y or d-m-y are automatically interpreted based on the separator: a slash (/) indicates American m/d/y format, while a dash (-) or dot (.) indicates European d-m-y format.
The above is the detailed content of How Can I Efficiently Convert dd/mm/YYYY Dates Using PHP's strtotime()?. For more information, please follow other related articles on the PHP Chinese website!