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:
- Create a date-time object, then use the modify function.
$dt = new DateTime("2019-08-14");
$dt->modify("+4 days");
$dt->modify("-1 week");
- 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"));
- 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!
TABLE OF CONTENTS
DOWNLOAD & NOTES
Here is the download link to the example code, so you don’t have to copy-paste everything.
EXAMPLE CODE DOWNLOAD
Just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
SORRY FOR THE ADS...
But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.
Buy Me A Coffee Code Boxx eBooks
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
<?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
<?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
<?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.
EXTRAS
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 toward 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 the current server time by default, or you can provide a date/time string. | Click Here |
$DATETIME->format(STRING) |
Formats the date-time object. | Predefined Standards |
$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 |
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!