PHP Convert Date Format: dd/mm/yyyy to yyyy-mm-dd
Converting dates between different formats can be a common task in programming. In PHP, it's also necessary to consider the ambiguity of dates in the form of dd/mm/yyyy or d-m-y, where the separator determines the format.
To convert a date from dd/mm/yyyy to yyyy-mm-dd, it's important to first determine the format of the input date. The default date function assumes the American m/d/y format. However, if the separator is a dash (-) or a dot (.), the European d-m-y format is assumed.
Solution:
$var = "20/04/2012"; echo date("Y-m-d", strtotime($var) );
$var = '20/04/2012'; $date = str_replace('/', '-', $var); echo date('Y-m-d', strtotime($date));
By using this approach, you can accurately convert dates between different formats in PHP.
The above is the detailed content of How to Convert Dates from dd/mm/yyyy to yyyy-mm-dd in PHP?. For more information, please follow other related articles on the PHP Chinese website!