Clue Mediator

Add or Subtract days, month and year to a date using PHP

๐Ÿ“…January 29, 2020
๐Ÿ—PHP

Today, we write a short tutorial on Add or Subtract days, month and year to a date using PHP.

php subtract minutes from datetime, php subtract days from today, php subtract date from today, php subtract date from current date, how to subtract time in php, php get year from date, php get month from date, Add days to a date, Add month to a date, Add year to a date , Add days to a current date, Add month to a current date, Add year to a current date.

Checkout more articles on PHP

Using php `date()` and `strtotime()` function we can easily add or subtract days, month and year to a given date. `date()` function formats a local date and time and returns the formatted date string.

Add or Subtract days, month and year to a date using PHP

  1. Add days to date
  2. Subtract days from date

Here, we will explain the simple way to do this task with example.

1. Add days to date

The following example shows how to add 7 days to date.

<?php
    $date = "2019-11-30";
    echo date('Y-m-d', strtotime($date. ' + 7 days'));
?>

// Output: 2019-12-07

Add 15 days to the current date.

<?php
    $date = date("Y-m-d");
    echo date('Y-m-d', strtotime($date. ' + 15 days'));
?>

// Output: 2020-01-21

Add 1 month to date.

<?php
    $date = "2020-01-06";
    echo date('Y-m-d', strtotime($date. ' + 1 month'));
?>

// Output: 2020-02-06

Add 1 year to date.

<?php
    $date = "2020-01-06";
    echo date('Y-m-d', strtotime($date. ' + 1 year'));
?>

// Output: 2021-01-06

2. Subtract days from date

The following example shows how to subtract 5 days from date.

<?php
    $date = "2020-01-04";
    echo date('Y-m-d', strtotime($date. ' - 5 days'));
?>

// Output: 2019-12-30

Subtract 2 days from current date.

<?php
    $date = date("Y-m-d");
    echo date('Y-m-d', strtotime($date. ' - 2 days'));
?>

// Output: 2020-01-04

Subtract 1 month from date.

<?php
    $date = "2020-01-06";
    echo date('Y-m-d', strtotime($date. ' - 1 month'));
?>

// Output: 2019-12-06

Subtract 1 year from date.

<?php
    $date = "2020-01-06";
    echo date('Y-m-d', strtotime($date. ' - 1 year'));
?>

// Output: 2019-01-06

That's it for today.
Thank you for reading. Happy Coding!