Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

PHP Dictionary

  1. Home
  2. PHP Dictionary
  3. DateTime Class

DateTime Class Since: PHP 5.3(2009)

A class for working with dates and times in an object-oriented way. It handles everything from creating and formatting date/time values to adding, subtracting, comparing, and calculating differences — all using method chaining.

Syntax

// Creates a DateTime object for the current or a specified date/time.
$dt = new DateTime($datetime, $timezone);

// Formats the date/time and returns it as a string.
$dt->format($format);

// Modifies the date/time using a relative date/time expression.
$dt->modify($modifier);

// Adds a DateInterval to the date/time.
$dt->add($interval);

// Subtracts a DateInterval from the date/time.
$dt->sub($interval);

// Returns the difference between two date/time values as a DateInterval.
$dt->diff($targetDateTime);

Method List

MethodDescription
new DateTime($datetime)Creates a DateTime object. If no argument is given, it defaults to the current date/time. A string argument is parsed the same way as strtotime().
format($format)Formats the date/time using the same format string as date().
modify($modifier)Changes the date/time using relative expressions such as "+1 day" or "next Monday". The object itself is updated in place.
add(DateInterval $interval)Adds time to the date/time using a DateInterval object.
sub(DateInterval $interval)Subtracts time from the date/time using a DateInterval object.
diff(DateTime $target)Returns the difference between two DateTime values as a DateInterval object.

Return Value

format() returns the formatted date/time as a string. modify(), add(), and sub() return the object itself, enabling method chaining. diff() returns a DateInterval object.

Sample Code

<?php
// Creates a DateTime object for the current date and time.
$now = new DateTime();
echo $now->format("Y-m-d H:i:s"); // Outputs the current date and time.

// Creates a DateTime object from a string.
$dt = new DateTime("2025-04-15 10:30:00");
echo $dt->format("Y-m-d H:i"); // Outputs "2025-04-15 10:30".

// Changes the date/time using modify().
$dt->modify("+1 month");
echo $dt->format("Y-m-d"); // Outputs "2025-05-15", one month later.

// Chains multiple operations together.
$result = (new DateTime("2025-01-01"))->modify("+6 months")->format("Y-m-d");
echo $result; // Outputs "2025-07-01".

// Adds and subtracts time using add() and sub() with DateInterval.
$dt2 = new DateTime("2025-04-15");
$dt2->add(new DateInterval("P10D")); // Adds 10 days.
echo $dt2->format("Y-m-d"); // Outputs "2025-04-25".

$dt2->sub(new DateInterval("P2M")); // Subtracts 2 months.
echo $dt2->format("Y-m-d"); // Outputs "2025-02-25".

// Calculates the difference between two dates using diff().
$start = new DateTime("2025-01-01");
$end = new DateTime("2025-04-15");
$interval = $start->diff($end);
echo $interval->days . " days"; // Outputs "104 days".
echo $interval->format("%m months and %d days"); // Outputs "3 months and 14 days".

// Practical example: calculating a person's age.
$birthday = new DateTime("1990-08-20");
$today = new DateTime();
$age = $birthday->diff($today);
echo $age->y . " years old"; // Outputs the current age.

// Creates DateTime objects with specific timezones.
$tokyo = new DateTime("now", new DateTimeZone("Asia/Tokyo"));
$utc = new DateTime("now", new DateTimeZone("UTC"));
echo $tokyo->format("H:i") . " / " . $utc->format("H:i"); // Compares Japan time and UTC.

// Using DateTimeImmutable prevents the original object from being modified.
$original = new DateTimeImmutable("2025-04-15");
$modified = $original->modify("+1 day"); // Returns a new object.
echo $original->format("Y-m-d"); // The original remains "2025-04-15".
echo $modified->format("Y-m-d"); // The modified version is "2025-04-16".

Notes

The DateTime class is an object-oriented API for date and time manipulation introduced in PHP 5.3. In addition to the same functionality as date() and strtotime(), it also supports advanced features such as timezone management and difference calculations.

DateTime objects are mutable, so calling modify(), add(), or sub() changes the object in place. To avoid unintended side effects, use DateTimeImmutable instead. DateTimeImmutable returns a new object on every method call, leaving the original unchanged.

The DateInterval format follows the ISO 8601 duration notation. For example, "P1Y2M3D" means 1 year, 2 months, and 3 days; "PT1H30M" means 1 hour and 30 minutes. When specifying a time component after P, you must insert a T before the time portion.

If you find any errors or copyright issues, please .