How do you handle dates and times in PHP

In PHP, handling dates and times is often done using the built-in DateTime class. This allows for easy manipulation and formatting of dates.

Example of Date and Time Handling in PHP

<?php // Create a new DateTime object $date = new DateTime('now'); // Format the date echo $date->format('Y-m-d H:i:s'); // Outputs current date and time ?>

You can modify dates easily as well:

<?php // Adding 1 day to the current date $date->modify('+1 day'); echo $date->format('Y-m-d H:i:s'); // Outputs date and time for the next day ?>

PHP DateTime Date Handling Time Manipulation Formatting Dates PHP Date Functions