Add & Subtract Days Weeks Months To A Date In PHP

Welcome to a quick tutorial on how to add and subtract days, weeks, months to a date in PHP. So you need to do some “date yoga” in your project?

There are 3 common ways to add and subtract from a date in PHP:

  1. Create a date-time object, then use the modify function.
    • $dt = new DateTime("2019-08-14");
    • $dt->modify("+4 days");
    • $dt->modify("-1 week");
  2. Use the strtotime() function.
    • $date = "2019-08-14";
    • $add = date("d M Y", strtotime($date . "+1 week"));
    • minus = date("d M Y", strtotime($date . "-2 months"));
  3. Manually calculate using the Unix timestamp.
    • $unix = strtotime("2019-08-14");
    • $add = date("d M Y", $unix + (2*86400));
    • $minus = date("d M Y", $unix - (14*86400));

That covers the basics, but let us walk through more examples in this guide – Read on!

ⓘ I have included a zip file with all the example source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes Add Minus Date Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

QUICK NOTES

If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download the source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

 

ADD & MINUS TO DATE

All right, let us now get into the examples of how to add/minus days, weeks, months to a date in PHP.

 

1) DATE TIME OBJECT

1-date-time.php
<?php
// (A) CREATE NEW DATE TIME OBJECT
$dt = new DateTime("2019-08-14");
echo "ORIGINAL DATE: " . $dt->format("d M Y") . "<br>";
 
// (B) ADD DAYS WEEKS MONTHS
$dt->modify("+4 days");
echo "+4 DAYS: " . $dt->format("d M Y") . "<br>";
$dt->modify("+2 weeks");
echo "+2 WEEKS: " . $dt->format("d M Y") . "<br>";
$dt->modify("+1 month");
echo "+1 MONTH: " . $dt->format("d M Y") . "<br>";
 
// (C) SUBTRACT DAYS WEEKS MONTHS
$dt->modify("-2 days");
echo "-2 DAYS: " . $dt->format("d M Y") . "<br>";
$dt->modify("-1 week");
echo "-1 WEEK: " . $dt->format("d M Y") . "<br>";
$dt->modify("-2 months");
echo "-2 MONTHS: " . $dt->format("d M Y") . "<br>";

This should be self-explanatory. The new DateTime(TIMESTAMP) object is probably one of the most painless and “human” ways to manipulate a given date. Simply use the format() function to add or subtract a range of days, weeks, months, years from the timestamp.

 

 

2) STRING TO TIME

2-strtotime.php
<?php
// (A) ORIGINAL DATE
$date = "2019-08-14";
echo "ORIGINAL DATE: $date<br>";
 
// (B) ADD DAYS WEEKS MONTHS
$added = date("d M Y", strtotime($date . "+4 days"));
echo "+4 DAYS: $added<br>";
$added = date("d M Y", strtotime($date . "+2 weeks"));
echo "+2 WEEKS: $added<br>";
$added = date("d M Y", strtotime($date . "+1 month"));
echo "+1 MONTH: $added<br>";
 
// (C) SUBTRACT DAYS WEEKS MONTHS
$added = date("d M Y", strtotime($date . "-4 days"));
echo "-4 DAYS: $added<br>";
$added = date("d M Y", strtotime($date . "-2 weeks"));
echo "-2 WEEKS: $added<br>";
$added = date("d M Y", strtotime($date . "-1 month"));
echo "-1 MONTH: $added<br>";

Yep, another simple one. This is actually a combination of using the strtotime() function to add/subtract the date, then using date() to get the date format that you want. I will leave links in the summary below on how date() works.

 

 

3) UNIX TIMESTAMP

3-unix.php
<?php
// (A) UNIX TIMESTAMP
// UNIX TIMESTAMP = NUMBER OF SECONDS SINCE 1 JAN 1970
$unix = strtotime("2019-08-14");
// 1 DAY = 24 HRS * 60 MINS * 60 SECS = 86400 SECS
$day = 86400;

// (B) ADD DAYS WEEKS MONTHS
echo "+4 DAYS: " . date("d M Y", $unix + (4*$day)) . "<br>";
echo "+2 WEEKS: " . date("d M Y", $unix + (14*$day)) . "<br>";
// BE CAREFUL: 1 MONTH MAY NOT BE EXACTLY 30 DAYS
echo "+1 MONTH: " . date("d M Y", $unix + (30*$day)) . "<br>";

// (C) SUBTRACT DAYS WEEKS MONTHS
echo "-3 DAYS: " . date("d M Y", $unix - (3*$day)) . "<br>";
echo "-2 WEEKS: " . date("d M Y", $unix - (14*$day)) . "<br>";
echo "-1 MONTH: " . date("d M Y", $unix - (30*$day)) . "<br>";

Lastly, for you guys who do not know – strtotime(TIMESTAMP)actually returns a Unix timestamp. That is, the number of seconds that have elapsed since 1 Jan 1970 UTC. So to add and subtract days/weeks/months with the Unix Timestamp is a very Mathematical process.

  • Essentially, one day has 24 hours (or 86400 seconds).
  • So for example, if we want to add 3 days to a timestamp, that will be $unix += 3 * 86400;
  • Another example, if we want to add 2 weeks, that will be 14 days – $unix += 14 * 86400.

Yep, you catch the drift.

 

 

USEFUL BITS & LINKS

That’s all for this tutorial, and here is a small section on some extras that may be useful to you.

 

WHICH IS THE BEST?

Personally, I am leaning towards DateTime and strtotime for convenience. But the performance of these 2 is not exactly amazing… Manually calculating with the Unix timestamp is not all bad, and it has an edge when it comes to processing in a loop – for ($i=1; i<14; i++) { $timestamp += 86400; }

 

SUMMARY – ALL THE COMMON DATE/TIME FUNCTIONS

Function Description Reference
$DATETIME = new DateTime(STRING) Create a new date-time object. Takes current server time by default, or provide a date/time string. Click Here
$DATETIME->format(STRING) Formats the date-time object. Predefined Standards

Manual Format

$DATETIME->modify(STRING) Add minutes, hours, days, weeks, months, years, from the given date/time. Click Here
strtotime(STRING) Gets the Unix timestamp from the given date string. Click Here

 

INFOGRAPHIC CHEAT SHEET

Add & Minus To Date In PHP (click to Enlarge)

 

THE END

Thank you for reading, and we have come to the end of this guide. I hope that it has helped you to understand better, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published.