DateTime Class

Create a new DateTime Object

Use the new() keyword

object(DateTime)#1 (3) { ["date"]=> string(26) "2024-12-05 23:47:49.760843" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "Europe/Madrid" }

The format() method

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

Thu 5th Dec 2024

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

Thu 12th Dec 2024
Wed 4th Dec 2024
Sun 9th Feb 2025
Tue 31st Dec 2024

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
1733438869

The modify() method

Change the DateTime object.

Tue 31st Dec 2024

Chaining - one method after another

Change the DateTime object.

Sun 1st Dec 2024

Methods return the Object

Take care when running methods as they return the object.

bool(true)