Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created May 26, 2023 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/7472ffa7af0e3dde2e4c1b69982ca231 to your computer and use it in GitHub Desktop.
Save code-boxx/7472ffa7af0e3dde2e4c1b69982ca231 to your computer and use it in GitHub Desktop.
PHP Add & Subtract To Date

PHP ADD & SUBTRACT DATE

https://code-boxx.com/add-subtract-date-php/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<?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>";
<?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>";
<?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>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment