How to add Days, Hours, Minutes, and Seconds to Datetime in PHP

Here we’ll provide the simplest way to add days, minutes, hours, and seconds to time using PHP. In PHP, using date() and strtotime() functions you can easily increase or decrease time. The provided PHP code lets you do the following work.

  • Add days to datetime.
  • Add hours to datetime.
  • Add minutes to datetime.
  • Add seconds to datetime.
$startTime date("Y-m-d H:i:s");

//display the starting time
echo 'Starting Time: '.$startTime;

//add 1 hour to time
$cenvertedTime date('Y-m-d H:i:s',strtotime('+1 hour',strtotime($startTime)));

//display the converted time
echo 'Converted Time (added 1 hour): '.$cenvertedTime;

//add 1 hour and 30 minutes to time
$cenvertedTime date('Y-m-d H:i:s',strtotime('+1 hour +30 minutes',strtotime($startTime)));

//display the converted time
echo 'Converted Time (added 1 hour & 30 minutes): '.$cenvertedTime;

//add 1 hour, 30 minutes and 45 seconds to time
$cenvertedTime date('Y-m-d H:i:s',strtotime('+1 hour +30 minutes +45 seconds',strtotime($startTime)));

//display the converted time
echo 'Converted Time (added 1 hour, 30 minutes  & 45 seconds): '.$cenvertedTime;

//add 1 day, 1 hour, 30 minutes and 45 seconds to time
$cenvertedTime date('Y-m-d H:i:s',strtotime('+1 day +1 hour +30 minutes +45 seconds',strtotime($startTime)));

//display the converted time
echo 'Converted Time (added 1 day, 1 hour, 30 minutes  & 45 seconds): '.$cenvertedTime;

Using the above code you can add time to current time or any desire time. To sub-track use the same code except - instead of +.

Add minutes to the current time:
Use the following code to add 5 minutes to the current time in PHP.

$cenvertedTime date('Y-m-d H:i:s'strtotime(' +5 minutes '));

You can use this reference to add seconds/minutes/hours/days time to the current DateTime with PHP.

12 Comments

  1. Wohan Said...
  2. Hakim Mirpour Said...
  3. Rajeev Singh Said...
  4. Muhammad Arslan Said...
  5. SK Foysal Said...
  6. Ashwin Juwekar Said...
  7. Dipti Said...
  8. Sergey Said...
  9. Akib Said...
  10. Ha Trung Said...
  11. Rachel Said...
  12. Combo Frenzy Said...

Leave a reply

keyboard_double_arrow_up