SyntaxStudy
Sign Up
Home PHP Reference DateTime class

DateTime class

class PHP 5.2

Object-oriented date/time handling with timezone support and easy arithmetic via modify() and diff().

Syntax

new DateTime(string $datetime = "now", ?DateTimeZone $timezone = null)

Example

php
<?php
$now = new DateTime();
echo $now->format('Y-m-d H:i:s');

$future = (new DateTime())->modify('+3 months');
echo $future->format('Y-m-d');

$diff = (new DateTime('2024-01-01'))->diff(new DateTime('2024-12-31'));
echo $diff->days . ' days'; // 365 days