Skip to content
PHP Logo

How can a date format like dd.mm.jj be converted to the yyyy-mm-dd format in PHP for database compatibility?

To convert a date format like dd.mm.jj to the yyyy-mm-dd format in PHP for database compatibility, you can use the strtotime() and date() functions. First, use strtotime() to convert the date string into a Unix timestamp, and then use date() to format the timestamp into the desired yyyy-mm-dd format.

php
$date = '31.12.21';
$new_date = date('Y-m-d', strtotime(str_replace('.', '-', $date)));
echo $new_date;