Add one month and subtract one month in PHP

Description

The following code shows how to add one month and subtract one month.

Example


<?php/*  www . j  a va  2 s  .  c  o  m*/
// create a DateTime object
$date = new DateTime('Aug 31, 2008');
echo '<p>Initial date is ' . $date->format('F j, Y') . '</p>';
// add one month
$date->modify('+1 month');
echo '<p>Add one month: ' . $date->format('F j, Y') . '</p>';
// subtract one month
$date->modify('-1 month');
echo '<p>Subtract one month: ' . $date->format('F j, Y') . '</p>';
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    Data Types »




Array
Array Associative
Array Util
ArrayObject
Data Types
Date
Date Format
DateTime
Number
String
String Escape
String Filter
String HTML
String Type
Timezone