Add or Subtract Days from Current Date in Php

In this tutorial, we’ll learn how to add or subtract days from the current date or date and time in PHP. To solve problems related to dates and times, PHP provides strtotime(), date_create(),date_add(), and date_sub() functions and a DateTime class. You can use strtotime() or DateTime class on your choice. We are going to use both of them in our today’s tutorials. We will subtract and add days, weeks, or months from/in the current date or custom date using PHP.

Table of contents:

Knowing the Problem related to add or subtract days from the current date

During web application, we need to play with dates and times many times. Just, for example, we are building an HR application and we need to calculate the employee’s salary based on working days from his joining date. All these will apply to the current time zone of your server. In server basically dates are stored in timestamp which is an integer value later on you can change this value into date and time. You can also formate the date as string-like YYYY-MM-DD after parsing the timestamp via strtotime() function. The strtotime() function is a built-in function in PHP which is used to convert an English textual date-time description to a UNIX timestamp.

Solution 1 – using strtotime() function

As discussed before, PHP provides strtotime() built-in function to solve the problem relating to date and time. Said function is very easy to use. With this function, you can add or subtracts days, months, and even years from a given date to get a new date. See the below example and then we will see how it works.

//Example 1 - Adds 7 days in current date
$date1 = date('M d, Y', strtotime('+7 days') ) ."</br>";
echo $date1;
//Example 2 - Subtracts 7 days from current date
$date2 = date('M d, Y', strtotime('-7 days') ) ."</br>";
echo $date2;

//Example 3 - Adds 7 months in current date
$date3 = date('M d, Y', strtotime('+7 months') ) ."</br>";
echo $date3;
//Example 4 - Subtracts 7 months from current date
$date4 = date('M d, Y', strtotime('-7 months') ) ."</br>";
echo $date4;

Explanation of solution 1

As you noticed the usage of this function is so easy. We used two functions date() and strtotime(). In the first example, we just added 7 days in the current date and in example 2 we subtract 7 days. Just like for example 1 and 2, we added and subtracted 7 months. You can add and subtracts years just like months and days. Just change months or days into years. Also, you noticed that in date() function we supplied format for the date. You can change this formate as you want. Here are some formate examples copied from PHP Official website. You also noticed a “</br>” tag at last of every code line, don’t worry about it. It just breaks the line and allows us to echo the result in the next line.

// Assuming today is March 10th, 2001, 5:16:18 pm, and that we are in the
// Mountain Standard Time (MST) Time Zone

$today = date("F j, Y, g:i a");                 // March 10, 2001, 5:16 pm
$today = date("m.d.y");                         // 03.10.01
$today = date("j, n, Y");                       // 10, 3, 2001
$today = date("Ymd");                           // 20010310
$today = date('h-i-s, j-m-y, it is w Day');     // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
$today = date('\i\t \i\s \t\h\e jS \d\a\y.');   // it is the 10th day.
$today = date("D M j G:i:s T Y");               // Sat Mar 10 17:16:18 MST 2001
$today = date('H:m:s \m \i\s\ \m\o\n\t\h');     // 17:03:18 m is month
$today = date("H:i:s");                         // 17:16:18
$today = date("Y-m-d H:i:s");                   // 2001-03-10 17:16:18 (the MySQL DATETIME format)

Solution 2 – using DateTime Class

We discussed earlier that we will also use DateTime class to Add or Subtract Days from Current Date. Using DateTime has significantly reduced the number of headaches. Here are some examples

// Example 1
$date = new DateTime("2017-05-18"); // For today/now, don't pass an arg.
$date->modify("+1 day");
echo $date->format("Y-m-d");

// Example 2
$date = new DateTime("2019-05-18"); // For today/now, don't pass an arg.
$date->modify("-1 day");
echo $date->format("Y-m-d");

Explanation of Solution 2

In above example we first just create new instance of DateTime class and put value in it. Then we used modify() method to add 1 day in the first example and then in the next example we subtract 1 day. There are other functions that you can use but the above examples are very simple. For your knowledge, I am giving 2 more examples that will use date_create(), date_add(), and date_sub() functions.

  // Example 1 -  Adds 10 days in given date
date_add($date,date_interval_create_from_date_string("10 days"));
echo date_format($date,"Y-m-d");

// Example 2 -  Subtracts 4 days in given date
date_sub($date,date_interval_create_from_date_string("4 days"));
echo date_format($date,"Y-m-d");

As you saw these functions also did the same as we noticed in previous codes. It is up to you which code do you like. You need to remember the syntax of any function. I hope you like today’s tutorial. Please share this post with your friends and family.

Here is another article related to date functions used in PHP.

One Thought on “Add or Subtract Days from Current Date in PHP”

Leave a Reply - I will show after approval