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


DateTime_Demo.php PHP SOURCE CODE:
Click to Reveal
<?php 

	if( $_GET["color"] !==""){
		// echo "GET['color'] = " . $_GET["color"] ;
		$BodyColor = $_GET["color"] ;
	}else{ $BodyColor = 'black' ;}
	if( $_GET["bgcolor"] !==""){
		// echo "GET['bgcolor'] = " . $_GET["bgcolor"] ;
		$BGColor = $_GET["bgcolor"] ;
		if(is_numeric($BGColor)){
			$BGColor = "#".$_GET["bgcolor"] ;
		}
	}else{ $BGColor = 'transparent' ;}	

	$getTime = $_SERVER['REQUEST_TIME']  ; 	

?>

<!DOCTYPE html>
<html lang= "en">
<head>
    <title>PHP DateTime Format Demo Page</title> 
	    <meta charset   = "UTF-8"> 
	    <meta copyright = "(c)Kirk Siqveland - Creative Commons CC-BY-NC-SA 4.0">
	<style>
	  body { 
	  		padding: 10px ;
	  		color: <?php echo $BodyColor?> ;
	  		background-color: <?php echo $BGColor?> ;
	  }
	  tab {
		     display: inline-block ;
		     width:            2em ;
	  }
	  .center {text-align: center;}
	</style>	
</head>
<body>
    <h1>PHP DateTime Format Demo Page</h1> 


	<strong>Let's start by getting a UNIX EPOCH Time in Integer format:</strong>
	<br/><br/><br/>
	In PHP the function - time()<br/>
	Returns the current date and time, measured in the number of seconds since the Unix Epoch start date/time<br/>
	January 1 1970 00:00:00 GMT<br/><br/>
	GMT is now considered a TimeZone, UTC is a Time Standard that coincides with GMT Time (No Daylight Savings)<br/>
	The Unix Epoch assumes UTC, which means no offsets.<br/><br/>

	The current <strong>time()</strong> is: <?php echo time();?> <br/><br/>

	We can also query the Server Explicitly:<br/>
	  $serverTime = $_SERVER['REQUEST_TIME'];<br/>
	  This returns: <strong><?php echo $_SERVER['REQUEST_TIME'];?></strong><br/><br/>

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

 	    date('r'); returns:  <strong><?php echo date('r');?></strong><br/><br/>
 	    date('r',time()); returns:  <strong><?php echo date('r',time());?></strong><br/><br/> 
 	  We can set a specific date-time using the Epoch value:<br/>	
 	    date('r', 327511920 ); returns:  <strong><?php echo date('r', 327511920);?></strong><br/><br/>


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

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

 	<br/><br/><hr/><br/><br/>
 	<h3>But if we want to manipulate the date, time or Time Zone values we need a DateTime object.</h3> 	

	Like any object we just need to create it:<br/>

	  $dto_now = new DateTime([date-time], [timezone]) ; //both paramaters are optional<br/>
	or<br/>
	  $dto_now = date_create([date-time], [timezone]); //both paramaters are optional<br/><br/>

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

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


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

	Or we can specify, date/time and timezone<br/><br/>


 	strtotime("11/22/2018 01:16:14 AM"); returns:  <strong><?php echo strtotime("11/22/2018 01:16:14 AM");?></strong><br/><br/>


	<h2>PHP DateTime Object / Formatting</h2>

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

	Let's Create a DateTime

	Create a DateTime variable:<br/><br/>
	<?php
		echo "Using PHP: attempting to find Server Time Zone...<br/>";
		// date_default_timezone_set();
		if (date_default_timezone_get()) { 
		    $_timezone = date_default_timezone_get() ;
			echo '   date_default_timezone_get() -- returned: ' . date_default_timezone_get() . '<br/>'; 
		}
		if (ini_get('date.timezone')) {
		    echo "   ini_get('date.timezone') -- returned: " . ini_get('date.timezone')  . '<br/>'; 
		}

		$utc_ztime=timezone_open('UTC');
		$utc_ztime=timezone_open('+00:00');
		echo 'UTC Timezone = '.timezone_name_get($utc_ztime). '<br/><br/>';		


		## This gets the current server time:
			$getTime  = $_SERVER['REQUEST_TIME']  ;
			$dtz0     = new DateTimeZone("+000");
			$dto_now  = new DateTime("@$getTime", $dtz0) ;   // convert UNIX timestamp to PHP DateTime
									// Note the "@" sign - this drops any time-zone offset.

		## OR as a demonstration of EPOCH 1, or some other dates, try any of these...
			// $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)


		// $utc_timez=timezone_open('-7:00');
		// $dto_now = date_create(@$ts,$utc_timez) ;
		// $dto_now = date_create('', $utc_timez) ;	
		// $dto_now = date_create(@$ts) ;	
		$dto_now = date_create() ;		
	?>	
	$_timezone = date_default_timezone_get() ; // Returns current Server Time Zone
	  Result: <?php echo chr(36)."_timezone = <strong>".$_timezone."</strong><br/>";?>

	$dto_now = date_create("now", timezone_open($_timezone)) ;


	<h2>Formatting DateTime...</h2>

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

	Here are several ways to get the same result:<br/><br/>
	$dto_now->format(\DateTime::RFC3339); Returns:<br/>
	     <?php echo $dto_now->format(\DateTime::RFC3339); ?><br/><br/>
	date(DATE_RFC3339); Returns:<br/> 
	     <?php echo date(DATE_RFC3339); ?><br/><br/>
	date(DATE_ATOM); Returns:<br/>
	     <?php echo date(DATE_ATOM); ?><br/>
	<tab></tab> //That's DATE_ATOM as the ATOM specification for RSS feed Date-Time format<br/><br/><br/>


	<h3>Now we can format our DateTime for display however we like: </h3><br/><br/>
	 <table>
	  <tr>
	  	<th>Key </th>
	    <th>To Get______________________</th>
	    <th>Format______________________</th>
	    <th>Returns:____________________</th>
	  </tr>

	  <tr></tr>	  

	  <tr>
	    <td class="center"> r </td>
	    <td>Date-time (RFC 2822)</td>
	    <td>date_format($dto_now,'r')</td>
	    <td><?php echo date_format($dto_now,'r'); ?></td>
	  </tr>

	  <tr>
	    <td class="center"> c </td>
	    <td>Date-time (ISO 8601)</td>
	    <td>date_format($dto_now,'c')</td>
	    <td><?php echo date_format($dto_now,'c'); ?></td>
	  </tr>

	  <tr>
	    <td class="center"> O </td>
	    <td>Date-time (GMT offset)</td>
	    <td>date_format($dto_now,'O')</td>
	    <td><?php echo date_format($dto_now,'O'); ?></td>
	  </tr>
	  <tr>
	    <td class="center"> P </td>
	    <td>Date-time (offset hours:minutes)</td>
	    <td>date_format($dto_now,'P')</td>
	    <td><?php echo date_format($dto_now,'P'); ?></td>
	  </tr>
	  <tr>
	    <td class="center"> U </td>
	    <td>Date-time Unix Epoch</td>
	    <td>date_format($dto_now,'U')</td>
	    <td><?php echo date_format($dto_now,'U'); ?></td>
	  </tr>	 
	  <tr>
	    <td class="center"> e </td>
	    <td>timezone identifier</td>
	    <td>date_format($dto_now,'e')</td>
	    <td><?php echo date_format($dto_now,'e'); ?></td>
	  </tr>	
	  <tr>  
	  	<td class="center"> T </td>
	  	<td>Time Zone in 3</td>
	    <td>date_format($dto_now,'T')</td>
	    <td><?php echo date_format($dto_now,'T'); ?></td>
	  </tr>		  
	  <tr>  
	  	<td class="center"> I </td>
	  	<td>Daylight Savings?</td>
	    <td>date_format($dto_now,'I')</td>
	    <td><?php echo date_format($dto_now,'I'); ?></td>
	  </tr>		  
	  <tr>
	    <td class="center"> L </td>
	    <td>Is it a Leap-Year?</td>
	    <td>date_format($dto_now,'L')</td>
	    <td><?php echo date_format($dto_now,'L'); ?></td>
	  </tr>		  
	  <tr> 
	  	<td> - </td>
	  </tr>	  
	  <tr> 
	  	<td>--Mix & Match:</td>
	  </tr>
	  <tr>
	  	<td class="center"> M/d/Y </td>
	    <td>Date (Month/Day/Year)</td>
	    <td>$dto_now->format('M/d/Y')</td>
	    <td><?php echo $dto_now->format('M/d/Y') ; ?></td>
	  </tr>	  

	  <tr>
	  	<td class="center"> d-m-Y </td>
	    <td>Date (Day-Month-Year)</td>
	    <td>$dto_now->format('d-m-Y')</td>
	    <td><?php echo $dto_now->format('d-m-Y') ; ?></td>
	  </tr>	 	  

	  <tr>
	  	<td class="center"> y-m-d </td>
	    <td>Date (Year-Month-Date)</td>
	    <td>$dto_now->format('y-m-d')</td>
	    <td><?php echo $dto_now->format('y-m-d') ; ?></td>
	  </tr>	 	

	  <tr>
	  	<td class="center"> Y-m-d H:i </td>
	    <td>Date and Time</td>
	    <td>$dto_now->format('Y-m-d H:i')</td>
	    <td><?php echo $dto_now->format('Y-m-d H:i') ; ?></td>
	  </tr>		  
	  <tr> 
	  	<td> - </td>
	  </tr>	 
	  <tr>
	    <td>--Each Part:</td>
	  </tr>

	  <tr>
	    <td class="center"> Y </td>
	    <td>4 digit Year</td>
	    <td>$dto_now->format('Y')</td>
	    <td><?php echo $dto_now->format('Y') ; ?></td>
	  </tr>		 
	  <tr>
	    <td class="center"> y </td>
	    <td>2 digit Year</td>
	    <td>$dto_now->format('y')</td>
	    <td><?php echo $dto_now->format('y') ; ?></td>
	  </tr>		
	  <tr>
	    <td class="center"> F </td>
	    <td>Month-Full Name</td>
	    <td>$dto_now->format('F')</td>
	    <td><?php echo $dto_now->format('M') ; ?></td>
	  </tr>		  
	  <tr>
	    <td class="center"> M </td>
	    <td>Month-3 Letters</td>
	    <td>$dto_now->format('M')</td>
	    <td><?php echo $dto_now->format('M') ; ?></td>
	  </tr>	
	  <tr>
	    <td class="center"> m </td>
	    <td>Month-Number 01-12</td>
	    <td>$dto_now->format('m')</td>
	    <td><?php echo $dto_now->format('m') ; ?></td>
	  </tr>	
	  <tr>
	    <td class="center"> n </td>
	    <td>Month-Number 1-12</td>
	    <td>$dto_now->format('n')</td>
	    <td><?php echo $dto_now->format('n') ; ?></td>
	  </tr>	
	  <tr>
	  	<td class="center"> l </td>
	  	<td>Day-of-Week</td>
	    <td>$dto_now->format('l')</td>
	    <td><?php echo $dto_now->format('l') ; ?></td>
	  </tr>	
	  <tr>
	  	<td class="center"> D </td>
	  	<td>Day-of-Week in 3</td>
	    <td>$dto_now->format('D')</td>
	    <td><?php echo $dto_now->format('D') ; ?></td>
	  </tr>	
	  <tr>  
	  	<td class="center"> w </td>
	  	<td>day 0=Sunday, 6=Saturday</td>
	    <td>$dto_now->format('w')</td>
	    <td><?php echo $dto_now->format('w') ; ?></td>
	  </tr>
	  <tr>  
	  	<td class="center"> z </td>
	  	<td>day of year 0-366</td>
	    <td>$dto_now->format('z')</td>
	    <td><?php echo $dto_now->format('z') ; ?></td>
	  </tr>	
	  <tr>  
	  	<td class="center"> d </td>
	  	<td>day number 01-31</td>
	    <td>$dto_now->format('d')</td>
	    <td><?php echo $dto_now->format('d') ; ?></td>
	  </tr>	
	  <tr>  
	  	<td class="center"> j </td>
	  	<td>day number 1-31</td>
	    <td>$dto_now->format('j')</td>
	    <td><?php echo $dto_now->format('j') ; ?></td>
	  </tr>		
	  <tr>  	
	  <tr>  
	  	<td class="center"> H </td>
	  	<td>Hour - 24</td>
	    <td>$dto_now->format('H')</td>
	    <td><?php echo $dto_now->format('H') ; ?></td>
	  </tr>		  
	  <tr>  
	  	<td class="center"> h </td>
	  	<td>hour - 12</td>
	    <td>$dto_now->format('h')</td>
	    <td><?php echo $dto_now->format('h') ; ?></td>
	  </tr>	
	  <tr>  
	  	<td class="center"> A </td>
	  	<td>AM PM</td>
	    <td>$dto_now->format('A')</td>
	    <td><?php echo $dto_now->format('A') ; ?></td>
	  </tr>	
	  <tr>  
	  	<td class="center"> a </td>
	  	<td>am pm</td>
	    <td>$dto_now->format('a')</td>
	    <td><?php echo $dto_now->format('a') ; ?></td>
	  </tr>	
	  <tr>  
	  	<td class="center"> i </td>
	  	<td>Minute</td>
	    <td>$dto_now->format('i')</td>
	    <td><?php echo $dto_now->format('i') ; ?></td>
	  </tr>	
	  <tr>  
	  	<td class="center"> s </td>
	  	<td>Second</td>
	    <td>$dto_now->format('s')</td>
	    <td><?php echo $dto_now->format('s') ; ?></td>
	  </tr>	
	  <tr>  
	  	<td class="center"> u </td>
	  	<td>Microsecond</td>
	    <td>$dto_now->format('u')</td>
	    <td><?php echo $dto_now->format('u') ; ?></td>
	  </tr>	

	</table> 	
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
</body>
</html>


Here is an iFrame running the DateTime_Demo.php file from this server

If you run the PHP on your server you will get something like this: