How to add and subtracting months using php date() function

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

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

<?php
$limit = 5; # No.of months 
$added_timestamp = strtotime('+'.$limit.' month', time()); 
$result = date('d.m.Y', $added_timestamp); 
echo $result;
?>

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

<?php
$limit = 5; # No.of months 
$added_timestamp = strtotime('-'.$limit.' month', time()); 
$result = date('d.m.Y', $added_timestamp); 
echo $result;
?>

In short form:

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

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

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

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

You Might Also Like

Leave a Reply

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