Skip to content
PHP Logo

How can the DateTime class in PHP be utilized to simplify date and time calculations in a script?

When working with date and time calculations in PHP, the DateTime class can simplify the process by providing a set of methods for manipulating dates and times. By using this class, you can easily perform operations such as adding or subtracting intervals, comparing dates, formatting dates, and more.

php
// Create a new DateTime object with the current date and time
$now = new DateTime();

// Add 1 day to the current date
$now->modify('+1 day');

// Format the date in a specific format
$formattedDate = $now->format('Y-m-d H:i:s');

echo $formattedDate;