How to add a specific number of hours or minutes to a DateTime object in PHP?

In PHP, you can use the DateTime class and its associated methods to add a specific number of hours or minutes to a DateTime object.

To add hours, you can use the modify() method with the + operator followed by the number of hours you want to add. Here's an example:

$datetime = new DateTime(); // create a new DateTime object with the current date and time $hours = 2; // number of hours to add $datetime->modify("+$hours hours"); echo $datetime->format('Y-m-d H:i:s'); // output the updated date and time

To add minutes, you can use the same modify() method but with the + operator followed by the number of minutes you want to add. Here's an example:

$datetime = new DateTime(); // create a new DateTime object with the current date and time $minutes = 30; // number of minutes to add $datetime->modify("+$minutes minutes"); echo $datetime->format('Y-m-d H:i:s'); // output the updated date and time

You can customize the format of the output by modifying the format parameter in the format() method as per your requirements.