PHP - DateTime Formatting | PHP-DateTime-Demo | Explained


Using the PHP - DateTime object

Rather than explain all of the details of what the DateTime object can tell us... it is easier to play with working Snippets:

Lets Start with UTC

(Read on for details but GMT is now a time zone, UTC is a Time Standard, but for us they work the same.):

echo gmdate("r") ; // Returns the Grenwich Mean date/time with no daylight Savings which is the same as UTC
                           // Fri, 03 Feb 2023 06:08:59 +0000

Now Let's Create a DateTime object from the local machine:

$date=date_create(); // Create a DateTime object using default values = Now.
        echo date_format($date,"r"); // print the date formatted to RFC 2822. 
                   // Fri, 03 Feb 2023 06:08:59 -0700  
                   // Notice this includes the Server Time Zone & Daylight savings offset, in this case of -7 hours

Create a DateTime object but assign a specific date:

$TimeStamp =  '1980-05-18T08:32 PDT'   ;	// May 18, 1980 at 8:32 a.m.
	$newDateTimeObject = date_create(@$TimeStamp ) ;	// The @-sign suppresses errors

	echo $newDateTimeObject->format('r') ;		// returns:  Sun, 18 May 1980 08:32:00 -0700 

You can leave-out a lot of information when creating the object, but when you do, you get assumed defaults filled in for you:

$TimeStamp =  'May 18th, 1980'   ;	       //
	$newDateTimeObject = date_create(@$TimeStamp ) ;	// The @-sign suppresses errors

	echo $newDateTimeObject->format('r') ;		// returns:  Sun, 18 May 1980 00:00:00 -0700 

Why does it have the UTC offset of -0700?
Because the server I am using has a default offset, and since I didn't specify, it filled it in for me!

The output here is in the format specified r = (RFC 2822)
But you probably want a different format. The neat thing is you can either use standard formats, or build your own from parts.
You just need to know the options...


Overview of Dates and Time:

Unix/Linux Servers use an EPOCH or Unix Time counter to tell time: According to Wikipedia it tallies how many seconds have elapsed since 1 January 1970 (Although, it is more correctly understood as since 31st December 1969, at Midnight.)

In other words an Epoch timestamp of 0000000001 OR 1, would represent the first second of January 1st, 1970.

So an Epoch value is a simple (large) integer. Negative values indicating further back in time...

The Microsoft datetime object also uses the same "0" date.

PHP - DateTime object - I have not been able to get an actual definition of what value is saved inside of the PHP DateTime object, but it allows a great deal of flexibility in assigning a value and outputting date/time information.


If you want a history lesson to better understand UTC/TimeZones/GMT etc. try this: Clock-Time Explained.



Now, lets hit the programmer basics:

UTC = Coordinated Universal Time, the International Time standard.

Atomic Clocks, set to a standard mark, seem clean and simple, and so as programmers we will keep our heads in the sand and stick with the "simple."

Whether or not they are correctly calibrated, these days we assume clocks are all coordinated to a single standard time; which is called UTC. Our clocks may be adjusted for our current location on the planet, and local "day-light-savings" laws, but they all coordinate to that single standard. If you want to go crazy, you could try to keep track of leap-seconds, leap-years, and historical changes to official time/dates - but for your sanity's sake we won't try that here...

A PHP DateTime Object, can contain all the information to synchronize to the UTC standard.
But it can only be as accurate as the information it has been assigned!

While the DateTime object holds that date/time information, it can report it to us in may formats - Which is what this snippet is all about.

Full Demo:

Okay, rather than go through things nitty-gritty I'm just going to give you the whole ball of wax:
I wrote a simple PHP page to go through all the format options so you can see how they look.

It is hosted here:

PHP-DateTime-Demo

(with Source of Course!)