How can DateTime be a better alternative to mktime when working with dates in PHP?

DateTime is a better alternative to mktime when working with dates in PHP because it provides a more object-oriented approach to handling date and time operations. DateTime offers a more intuitive and flexible way to manipulate dates, compared to the procedural style of mktime. Using DateTime can lead to cleaner and more maintainable code.

// Using DateTime to work with dates in PHP
$date = new DateTime();
$date->setDate(2022, 12, 31);
$date->setTime(23, 59, 59);

echo $date->format('Y-m-d H:i:s');