DateTime Class

Create a new DateTime Object

Use the new() keyword

object(DateTime)#1 (3) { ["date"]=> string(26) "2025-07-20 11:59:13.334572" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "Europe/Madrid" }

The format() method

The format() method uses the same format flags as procedural date()

Sun 20th Jul 2025

Headline format characters

d
Day of the month, 2 digits with leading zeros 01 to 31
D
A textual representation of a day, three letters Mon through Sun
j
Day of the month without leading zeros 1 to 31
l
(lowercase 'L') A full textual representation of the day of the week Sunday through Saturday
F
A full textual representation of a month, such as January or March January through December
m
Numeric representation of a month, with leading zeros 01 through 12
M
A short textual representation of a month, three letters Jan through Dec
n
Numeric representation of a month, without leading zeros 1 through 12
t
Number of days in the given month 28 through 31
Y
A full numeric representation of a year, 4 digits Examples: 1999 or 2003
y
A two digit representation of a year Examples: 99 or 03

Creating a Specfic Date - European Style

Can use DD-MM-YYYY

Sun 14th Oct 1066

Creating a Specfic Date - US Style

Can use MM/DD/YYYY

Sun 20th Jul 1969

Creating a Specfic Date - ISO Style

Can use YYYY-MM-DD

Mon 25th Dec 2017

Relative Date Creation

Reserved words for creating relative dates

Sun 27th Jul 2025
Sat 19th Jul 2025
Wed 24th Sep 2025
Thu 31st Jul 2025

The getTimestamp() method

Retrieve as UNIX time stamp. Check whether 32 or 64 installation with PHP_INT_SIZE. 64-bit version of PHP echos 8, 32-bit version of PHP echos 4

8
1753005553

The modify() method

Change the DateTime object.

Thu 31st Jul 2025

Chaining - one method after another

Change the DateTime object.

Tue 1st Jul 2025

Methods return the Object

Take care when running methods as they return the object.

bool(true)