PHP DateTime Format Demo Page

Let's start by getting a UNIX EPOCH Time in Integer format:


In PHP the function - time()
Returns the current date and time, measured in the number of seconds since the Unix Epoch start date/time
January 1 1970 00:00:00 GMT

GMT is now considered a TimeZone, UTC is a Time Standard that coincides with GMT Time (No Daylight Savings)
The Unix Epoch assumes UTC, which means no offsets.

The current time() is: 1736029679

We can also query the Server Explicitly:
  $serverTime = $_SERVER['REQUEST_TIME'];
  This returns: 1736029679

We can use date() to set the display format of an EPOCH integer value, making it human-readable:
date() takes two parameters, the first identifies the format we want, the second is the EPOCH integer.
  If you leave off the EPOCH value, PHP uses the current date-time (from your server).

    date('r'); returns: Sat, 04 Jan 2025 14:27:59 -0800

    date('r',time()); returns: Sat, 04 Jan 2025 14:27:59 -0800

  We can set a specific date-time using the Epoch value:
    date('r', 327511920 ); returns: Sun, 18 May 1980 08:32:00 -0700

The last segment of this date format indicate TimeZone offset,
which defaults to your server's default TimeZone.

For Simple representation we can work with an EPOCH value and date() for formatting.




But if we want to manipulate the date, time or Time Zone values we need a DateTime object.

Like any object we just need to create it:
  $dto_now = new DateTime([date-time], [timezone]) ; //both paramaters are optional
or
  $dto_now = date_create([date-time], [timezone]); //both paramaters are optional

Both the date_create() and the new DateTime() functions use a string-parser in PHP to interpret the [date-time]
There is a good bit of flexibility but something like:

// $ts = '01-01-1970 00:00:01 +0000' ; // Epoch Value of 1
// $ts = '1980-05-18 08:32 -0700' ; // May 18, 1980 at 8:32 a.m.
// $ts = '06-12-1895 13:55:01 +0100' ; // June 12, 1895 at 1:55 p.m. (in Norway)
$dto_now = date_create(@$ts) ;

If we leave it blank we get current date/time, and the server's timezone:
  $dto_now = date_create();

Or we can specify, date/time and timezone

strtotime("11/22/2018 01:16:14 AM"); returns: 1542878174

PHP DateTime Object / Formatting

To make the DateTime object human-readable, we need to give it a format.
There are many predefined formats, or you can build your own.

Let's Create a DateTime Create a DateTime variable:

Using PHP: attempting to find Server Time Zone...
   date_default_timezone_get() -- returned: America/Los_Angeles
   ini_get('date.timezone') -- returned: America/Los_Angeles
UTC Timezone = +00:00

$_timezone = date_default_timezone_get() ; // Returns current Server Time Zone   Result: $_timezone = America/Los_Angeles
$dto_now = date_create("now", timezone_open($_timezone)) ;

Formatting DateTime...

The Formatting is defined using certain presets, either for a whole date or for DateTime elements.

Here are several ways to get the same result:

$dto_now->format(\DateTime::RFC3339); Returns:
     2025-01-04T14:27:59-08:00

date(DATE_RFC3339); Returns:
     2025-01-04T14:27:59-08:00

date(DATE_ATOM); Returns:
     2025-01-04T14:27:59-08:00
//That's DATE_ATOM as the ATOM specification for RSS feed Date-Time format


Now we can format our DateTime for display however we like:



Key To Get______________________ Format______________________ Returns:____________________
r Date-time (RFC 2822) date_format($dto_now,'r') Sat, 04 Jan 2025 14:27:59 -0800
c Date-time (ISO 8601) date_format($dto_now,'c') 2025-01-04T14:27:59-08:00
O Date-time (GMT offset) date_format($dto_now,'O') -0800
P Date-time (offset hours:minutes) date_format($dto_now,'P') -08:00
U Date-time Unix Epoch date_format($dto_now,'U') 1736029679
e timezone identifier date_format($dto_now,'e') America/Los_Angeles
T Time Zone in 3 date_format($dto_now,'T') PST
I Daylight Savings? date_format($dto_now,'I') 0
L Is it a Leap-Year? date_format($dto_now,'L') 0
-
--Mix & Match:
M/d/Y Date (Month/Day/Year) $dto_now->format('M/d/Y') Jan/04/2025
d-m-Y Date (Day-Month-Year) $dto_now->format('d-m-Y') 04-01-2025
y-m-d Date (Year-Month-Date) $dto_now->format('y-m-d') 25-01-04
Y-m-d H:i Date and Time $dto_now->format('Y-m-d H:i') 2025-01-04 14:27
-
--Each Part:
Y 4 digit Year $dto_now->format('Y') 2025
y 2 digit Year $dto_now->format('y') 25
F Month-Full Name $dto_now->format('F') Jan
M Month-3 Letters $dto_now->format('M') Jan
m Month-Number 01-12 $dto_now->format('m') 01
n Month-Number 1-12 $dto_now->format('n') 1
l Day-of-Week $dto_now->format('l') Saturday
D Day-of-Week in 3 $dto_now->format('D') Sat
w day 0=Sunday, 6=Saturday $dto_now->format('w') 6
z day of year 0-366 $dto_now->format('z') 3
d day number 01-31 $dto_now->format('d') 04
j day number 1-31 $dto_now->format('j') 4
H Hour - 24 $dto_now->format('H') 14
h hour - 12 $dto_now->format('h') 02
A AM PM $dto_now->format('A') PM
a am pm $dto_now->format('a') pm
i Minute $dto_now->format('i') 27
s Second $dto_now->format('s') 59
u Microsecond $dto_now->format('u') 430657