Home > Backend Development > PHP Tutorial > How Can I Use `strtotime()` with Dates in 'dd/mm/YYYY' Format?

How Can I Use `strtotime()` with Dates in 'dd/mm/YYYY' Format?

Mary-Kate Olsen
Release: 2024-12-25 20:09:14
Original
613 people have browsed it

How Can I Use `strtotime()` with Dates in

strtotime() Format Discrepancy

The strtotime() function offers an efficient method for converting strings representing dates and times into timestamps. However, users may encounter a formatting issue when dealing with dates in "dd/mm/YYYY" format, as this is not directly supported by the function.

Instead, strtotime() recognizes only the "mm/dd/YYYY" format. To address this issue and convert dates in "dd/mm/YYYY" format, an alternative approach is required. Here's a simple solution:

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

This code replaces the slashes with dashes in the input date string, effectively converting it to the supported "mm-dd-YYYY" format. Then, it uses strtotime() to convert the modified date to a timestamp. Finally, the date('Y-m-d') function is employed to format the timestamp as a string in the desired "YYYY-mm-dd" format.

It's worth noting that the strtotime documentation explicitly states that dates in "m/d/y" or "d-m-y" formats are interpreted differently based on the separator used. A slash (/) indicates the American "m/d/y" format, while a dash (-) or dot (.) signifies the European "d-m-y" format.

The above is the detailed content of How Can I Use `strtotime()` with Dates in 'dd/mm/YYYY' Format?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template