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

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

<?php
  $limit = 5; # No.of years

  $added_timestamp = strtotime('+'.$limit.' year', time());

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

  echo $result;
?>

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

<?php
  $limit = 5; # No.of years

  $added_timestamp = strtotime('-'.$limit.' year', time());

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

  echo $result;
?>

In short form:

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

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

?>

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

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

?>

You Might Also Like

Leave a Reply

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