How to add and subtracting days using php date function

PHP date() function formats a local date and time, and returns the formatted date string. In this article, I described the simple process about how to add and subtract number of days from current date using default PHP date() function.

Add number of days in current date: php strtotime add days (Example: adding 5 days)

<?php
  $day_limit = 5; # No.of days

  $added_timestamp = strtotime('+'.$day_limit.' day', time());

  $result = date('d.m.Y', $added_timestamp);

  echo $result;
?>

Subtract number of days in current date: php strtotime subtract days (Example: subtracting 5 days)

<?php
 $day_limit = 5; # No.of days

 $subtracted_timestamp = strtotime('-'.$day_limit.' day', time());

 $result = date('d.m.Y', $subtracted_timestamp);

 echo $result;
?>

In short form:

Add number of days in current date: (Example: adding 5 days)

<?php
 
 echo date('d.m.Y', strtotime('+5 day', time()));

?>

Subtract number of days in current date: (Example: subtracting 5 days)

<?php
 
 echo date('d.m.Y', strtotime('-5 day', time()));

?>

You Might Also Like

Leave a Reply

Your email address will not be published. Required fields are marked *