How to add and subtracting weeks 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 weeks from current date using default PHP date() function.

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

<?php
  $week_limit = 5; # No.of weeks

  $added_timestamp = strtotime('+'.$week_limit.' week', time());

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

  echo $result;
?>

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

<?php
 $week_limit = 5; # No.of weeks

 $subtracted_timestamp = strtotime('-'.$week_limit.' week', time());

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

 echo $result;
?>

In short form:

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

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

?>

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

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

?>

You Might Also Like

Leave a Reply

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