Some Built In Functions for Dates and Times

 

Introduction.  Now we will venture into the realm of dealing with dates and times.  Here my listing of built in functionality will be somewhat more thorough. 

The overall function looks like the following.

date( )

What is different about this function is that there are many different parameters that can be set within the function.  Most can be set in just about any combination with the others to get just about any representation of a Date/Time that is desired.

For example, to get one fairly standard representation of the  current date/time you would use

date('F j, Y :H:i:s A'); which gives January 8, 2003:8:42:11 AM

The following table contains a summary.  We will work some examples after this.  Notice the case of the parameter is important.

 

Parameter Description Example
a am or pm am
A AM or PM PM
B Swatch Internet time  
d day of the month as two digits 08
D day of the week as three letters Mon
F month April
g hours in 12 hour format as one or two digits 6
G hours in 24 hour format as one or two digits 7 or 21
h hours in 12 hour format as two digits 07
H hours in 24 hour format as two digits 07 or 22
i minutes 45
I daylight savings time 1 or 0
j day of the month as one or two digits 5
l (lowercase L) day of the week Monday
L Boolean for whether it is a leap year 0 or 1
m month as two digits 03 or 11
M month as three letters Apr
n month as one or two digits 3 or 11
O hours from GMT - Greenwich Mean Time +0500
r standard date:time  
s seconds 03
S English ordinal suffix - st, nd, rd, th  
t number of days in a given month 28 or 30 or 31
T time zone setting of server EST or CST
U seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)  
w day of the week numeric 0 - Sunday
6 - Saturday
W ISO-8601 week number of year
weeks starting on Monday
 
y year as two digits 04
Y year as four digits 2004
z day of the year 0 to 365
Z time zone offset in seconds  

 

 

The following script should be called date_time.php.

 

<html>
<head>
<title>Some Built In Date/Time Functions</title>
</head>

<body>
<?php
// obtaining the current date and parsing it
$date_time = date("r");
print "The current date - time is <b>$date_time </b><BR>\n";
print "notice the time zone offset is based on the location of the server.<BR><BR>\n";
print "<B>Some example parsings</B><BR><BR>\n";
$am_pm = date("A");
print "Whether it is AM or PM is <b>$am_pm </b><BR>\n";
$dst = date("I");
print "Whether it is daylight savings time is <b>$dst </b><BR>\n";
$time_zone = date("T");
print "The time zone for the server is <b>$time_zone </b><BR>\n";
$leap = date("L");
print "Whether it is a leap year is <b>$leap </b><BR>\n";
$day_in_year = date("z");
print "What day it is in the year from 1 to 365 is <b>$day_in_year </b><BR>\n";
$month_name = date("F");
print "Parsing out the name of the month gives <b>$month_name </b><BR>\n";
$month_number = date("m");
print "Parsing out the number for the month gives <b>$month_number </b><BR>\n";
$month_days = date("t");
print "The number of days in this month <b>$month_days </b><BR>\n";

?>
</body>
</html>

 

When uploaded and run you should see the following screen on your computer.