Adding Days or Hours to DateTime in PHP

Sovary May 31, 2022 1.12K
2 minutes read

To calculate date by adding number of days, months or even years, such as how do we know 15 days after today what date is it? there are different way to find datetime after adding or subtract month and return result as new datetime calculated. In this tutorial I will show how to adding number years, days on any specific datetime in PHP with examples.

Also read

Adding Datetime

Example #1 - Using date_add()

<?php
  // create datetime object
  $date=date_create("2022-05-31 5:52:44 AM");
  // add 22 days on datetime object
  date_add($date,date_interval_create_from_date_string("22 days"));
  //display result in custom format
  echo date_format($date,"Y-m-d h:i:s A");
?>

Output

2022-06-22 05:52:44 AM

date_interval_create_from_date_string() will return an DateInterval object by following relative parts of the string.

Example #2 - Using date_add()

<?php
  // create datetime object
  $date= date_create("2022-05-31 5:52:44 AM");
  echo "Date => ".date_format($date,"Y-m-d h:i:s A");
  $day =15;
  $hour = 8;
  $minute = 11;
  // add days hours minutes on datetime object
  date_add($date,date_interval_create_from_date_string("$day days $hour hours $minute minutes"));
  //display result in custom format yyyy/mm/dd hour:minute:second
  echo "\nDate added => ".date_format($date,"Y-m-d h:i:s A");
?>

Output

Date => 2022-05-31 05:52:44 AM
Date added => 2022-06-15 02:03:44 PM

Example #3 - Using strtotime()

<?php
  // String datetime
  $date = '2022-05-31 5:52:44 AM';
  echo "Date => ". $date; 
  // Add Days and Convert to timestamp value.
  $timestamp = strtotime($date . ' +1 day 2 hour 3 minutes 2 second');
  // Convert timestamp to datetime format
  $date = date('Y-m-d H:i:s A', $timestamp);
  //display result
  echo "\nDate added => ".$date;
?>

Output

Date => 2022-05-31 5:52:44 AM
Date added => 2022-06-01 07:55:46 AM

Example #4 - Using modify()

<?php
  // Create datetime object
  $dt = new DateTime("2022-05-31 5:52:44 AM");
  // add 4 days 5 hour 1 minute 
  $dt->modify("+4 days 5 hour 1 minute");
  // print result with custom format
  echo "Date added => ".date_format($dt,'Y-m-d H:i:s A');
?>

Output

2022-06-04 10:53:44 AM

Subtraction Datetime

Example #1 - Using date_sub()

​
<?php
  // create datetime object
  $date= date_create("2022-05-31 5:52:44 AM");
  echo "Date => ".date_format($date,"Y-m-d h:i:s A");
  $day =15;
  $hour = 8;
  $minute = 11;
  // subtract days hours minutes on datetime object
  date_sub($date,date_interval_create_from_date_string("$day days $hour hours $minute minutes"));
  //display result in custom format yyyy/mm/dd hour:minute:second
  echo "\nDate subtract => ".date_format($date,"Y-m-d h:i:s A");
?>

Output

Date => 2022-05-31 05:52:44 AM
Date subtract => 2022-05-15 09:41:44 PM

--adres-

Example #2 - Using strtotime() 

<?php
  // String datetime
  $date = '2022-05-31 5:52:44 AM';
  echo "Date => ". $date; 
  // Subtract Days and Convert to timestamp value.
  $timestamp = strtotime($date . ' -1 day 2 hour 3 minutes 2 second');
  // Convert timestamp to datetime format
  $date = date('Y-m-d H:i:s A', $timestamp);
  //display result
  echo "\nDate subtract => ".$date;
?>

Output

Date => 2022-05-31 5:52:44 AM
Date subtract => 2022-05-30 07:55:46 AM

 Example #3 - Using modify()

<?php
  // Create datetime object
  $dt = new DateTime("2022-05-31 5:52:44 AM");
  // subtract 4 days 5 hour 1 minute 
  $dt->modify("-4 days 5 hour 1 minute");
  // print result with custom format
  echo "Date subtract => ".date_format($dt,'Y-m-d H:i:s A');
?>

Output

Date subtract => 2022-05-27 10:53:44 AM

Hope your will learn how to add days to datetime in PHP with these simple examples. Thank you

PHP 
Author

As the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him