Quick way to add hours and minutes with PHP

Quick way to add hours and minutes with PHP

The following is really easy way to add days, minutes, hours and seconds to a time using PHP. Using the date function to set the format of the date to be returned then using strtotime to add the increase or decrease of time then after a comma use another strtotime passing in the start date and time.

//set timezone
date_default_timezone_set('GMT');

//set an date and time to work with
$start = '2014-06-01 14:00:00';

//display the converted time
echo date('Y-m-d H:i',strtotime('+1 hour +20 minutes',strtotime($start)));

Times can be entered in a readable way:

  • +1 day = adds 1 day
  • +1 hour = adds 1 hour
  • +10 minutes = adds 10 minutes
  • +10 seconds = adds 10 seconds

To sub-tract time its the same except a - is used instead of a +