PHP Script to add or subtract days, months, years to a Date!


Here is another Interesting PHP script which you can use it in your project. A PHP script to add or subtract days, months and years to date.



Adding days or months or year to a date in PHP is a piece of cake if you are an experienced PHP person. Just see the snippet below:

//this will add a day to the current date
strtotime('+ 1 days');

//this will add a month to the current date
strtotime('+ 1 month'); 

//this will add a year to the current date
strtotime('+ 1 year'); 


Easy right? But I have elaborated the above snippet to a useful PHP script along with HTML and Ajax. You can modify and use it in your projects if you wish to.

HTML and jQuery part (Front end – index.php):

        <title>PHP Script to add or subtract days, months, year to a Date!</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src=""></script>
        //on click of the Find new date button
          //send the form data to caldata.php
          $.post('caldate.php', $('#myform').serialize(), function(res){
            //display the result from caldate.php
        <h2>PHP Script to add or subtract days, months, year to a Date!</h2>
    <form action="" name="myform" id="myform" method="post">
      Select a date:  <input type="date" name="selDate" value="<?php echo date('Y-m-d'); ?>" /><br /><br />
      Add or Sub <select name="addsub" style="padding: 3px;"><option>+</option><option>-</option></select><br /><br />
      Days <input type="number" min="0" max="10000" value="0" name="days" />&nbsp;
      Months <input type="number" min="0" max="10000" value="0" name="months" />&nbsp;
      Years <input type="number" min="0" max="10000" value="0" name="years" />&nbsp;
      &nbsp;<input type="button" value="Find new date!" id="findNewDate" /><br /><br />
      <div id="result"></div>

Read the in-line comments in the above program to understand the script.

PHP date calculation script: (Backend – caldate.php):

  //capture the form data
  $seldate = $_POST['selDate'];
  $addorsub = $_POST['addsub'];
  $days = $_POST['days'];
  $months = $_POST['months'];
  $years = $_POST['years'];
  //in a single line we are adding days, months and years to the $selDate (selected date)
  $addDays = strtotime($seldate . $addorsub. $days .' days'. $addorsub . $months. ' months'. $addorsub . $years . ' years');

  //ouput the result
  print "<b>Selected Date: </b>". date('m-d-Y', strtotime($seldate)) . "<br />";
  print "<b>New Date: </b>". date('m-d-Y', $addDays) . "<br />";


Above Frontend and Backend script can be written in a single page, but I have implemented Ajax to not to refresh the page after every click of the ‘Find New Date’ button.

You can see the demo here:


And you can download the script from below as a zip file:


One thought on “PHP Script to add or subtract days, months, years to a Date!

  1. This is exactly the script I’ve been looking for, but I am confused by the terminology of front end and back end script. I’m a dabbler in programming and am having trouble figuring out how these two scripts work together to get the end result that you have posted above. Thanks for your obvious commitment to providing tutorials and your help with this one.

Leave a Reply

Theme: Overlay by Kaira