How can the DateTime, DateInterval, and DatePeriod classes in PHP be utilized for more accurate time-related operations compared to the date() function?
Using the DateTime, DateInterval, and DatePeriod classes in PHP allows for more accurate time-related operations compared to the date() function because they provide a more object-oriented approach to working with dates and times. These classes offer a wider range of functionality, including the ability to easily perform calculations, comparisons, and manipulations on dates and times.
// Creating a DateTime object for the current date and time
$currentDateTime = new DateTime();
// Creating a DateInterval object to add 1 day to the current date
$interval = new DateInterval('P1D');
// Adding the interval to the current date
$currentDateTime->add($interval);
// Formatting and displaying the new date
echo $currentDateTime->format('Y-m-d');