Skip to content
PHP Logo

What are the advantages of using strtotime() function over mktime() when working with dates in PHP?

When working with dates in PHP, using the strtotime() function is advantageous over mktime() because strtotime() can handle a wider range of date formats and is more versatile in parsing date strings. It also automatically adjusts for time zones, making it easier to work with dates across different time zones.

php
// Example of using strtotime() function to work with dates
$dateString = "2022-12-31";
$timestamp = strtotime($dateString);
echo date("Y-m-d", $timestamp);