1. Computing

Learn PHP - All About PHP Functions

By

2 of 5

Time and Date
PHP has the ability to dynamically generate the time and date. Using a simple line of code we are able to include this on our site, however it is important to know how the formatting works.

<?php print time(); ?> 

The above code outputs a long string of numbers. What these numbers represent is the time based in the amount of seconds that have passed since January 1 1970 00:00:00 GMT. This number can also be assigned to a variable:

 <?php 
 $b = time (); 
 print $b; 
 ?> 

Although this is a handy feature, sometimes you want a more formatted and human friendly representation of the date. You can use the date function in conjunction with the time function to display this in the format of date ( format , time ) In our case we want the start time to be now, so we will call the time first. We will demonstrate many different types of formatting

 <?php 
 $b = time (); 
 print date("m/d/y",$b) . "<br>"; 
 print date("D, F jS",$b) . "<br>"; 
 print date("l, F jS Y",$b) . "<br>"; 
 print date("g:i A",$b) . "<br>"; 
 print date("r",$b) . "<br>"; 
 print date("g:i:s A D, F jS Y",$b) . "<br>"; 
 ?> 

When you run this code you will see that the information is formatted in many different ways. What each of the letters means for formatting is explained on the next page.

Related Video
Basic PHP Syntax
Using PHP With HTML
  1. About.com
  2. Computing
  3. PHP / MySQL
  4. Learn PHP
  5. PHP Date & Time functions

©2014 About.com. All rights reserved.