Date and Time Functions
Last but certainly not least, I'll introduce a couple of PHP's date- and time-related functions. The most important of these is the aptly named date() function, which returns a string of text for a certain date and time according to a format you specify.
date (format, [timestamp]);
The timestamp is an optional argument representing the number of seconds since the Unix Epoch (midnight on January 1, 1970) for the date in question. If not specified, PHP will just use the current time on the server.
There are myriad formatting parameters available (Table 3.1) and these can be used in conjunction with straight text. For example,
echo date('F j, Y'); // January 21, 2003 echo date('H:i'); // 23:14 echo date('D'); // Mon
You can find the timestamp for a particular date using the mktime() function.
int mktime (hour, minute, second, month, day, year);
Finally, the getdate() function can be used to return an array of values (Table 3.2) for the current date and time. For example,
$dates = getdate(); echo $dates['month']; // January
Table 3.1. The date() function can take any combination of these parameters to alter its returned results.
Date Function Formatting |
||
CHARACTER |
MEANING |
EXAMPLE |
Y |
year as 4 digits |
2003 |
y |
year as 2 digits |
03 |
n |
month as 1 or 2 digits |
2 |
m |
month as 2 digits |
02 |
F |
month |
February |
M |
month as 3 letters |
Feb |
j |
day of the month as 1 or 2 digits |
8 |
d |
day of the month as 2 digits |
08 |
l (lowercase L) |
day of the week |
Monday |
D |
day of the week as 3 letters |
Mon |
g |
hour, 12-hour format as 1 or 2 digits |
6 |
G |
hour, 24-hour format as 1 or 2 digits |
18 |
h |
hour, 12-hour format as 2 digits |
06 |
H |
hour, 24-hour format as 2 digits |
18 |
i |
minutes |
45 |
a |
am or pm |
am |
A |
AM or PM |
PM |
s |
seconds |
18 |
Table 3.2. The getdate() function returns this associative array.
The getdate() Array |
||
KEY |
VALUE |
EXAMPLE |
year |
year |
2003 |
mon |
month |
12 |
month |
month name |
December |
mday |
day of the month |
25 |
weekday |
day of the week |
Tuesday |
hours |
hours |
11 |
minutes |
minutes |
56 |
seconds |
seconds |
47 |
To use the date functions:
- Create a new PHP document in your text editor (Script 3.16).
<?php # Script 3.16 - dateform.php $page_title = 'Calendar Form'; include ('./header.inc');
- Begin defining a function.
function make_calendar_pulldown ($this_month = NULL, $today = NULL, $year = NULL) {
This script will be like the earlier dateform.php script (refer to Script 3.5) but will preselect values in the pull-down menus based upon the current date. The function takes up to three arguments, all of which are optional (since they're initially set to NULL). - Create the month pull-down menu.
$months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'); echo '<select name="month">'; foreach ($months as $key => $value) { echo "<option value=\"$key\""; if ($key == $this_month) { echo ' selected="selected"'; } echo ">$value</option>\n"; }
The process for making the pull-down menu has not changed except for the addition of the conditional inside of the foreach loop. This conditional will check the current month (as a number between 1 and 12) against a submitted value for this month. If they are the same, then the HTML code is added so that this month will be selected. - Make the day pull-down menu.
echo '</select> <select name="day">'; for ($day = 1; $day <= 31; $day++) { echo "<option value=\"$day\""; if ($day == $today) { echo ' selected="selected"'; } echo ">$day</option>\n"; }
Like the month pull-down menu, this section of the code is the same except for the added conditional. - Make the year pull-down menu.
echo '</select> <select name="year">'; if (!isset($year)) { $year = date('Y'); } $end = $year + 10; while ($year <= $end) { echo "<option value=\"$year\">$year</option>\n"; $year++; } echo '</select>';
The year pull-down menu is slightly different in that it's set up to create pull-down menus from the submitted year for ten more. If the submitted year was not set, the current year (date('Y')) will be used. - Complete the function and create the initial form tag.
} echo '<form action="dateform.php" method="post">';
- Get the current date information and call the function.
$dates = getdate(); make_calendar_pulldown ($dates['month'], $dates['mday'], $dates['year']);
To send the month, day, and year parameters to the function, I'll first use the getdate() function to retrieve an array of values for today. Then I call the function, referring to the appropriate array elements. - Complete the form and print the current day and time.
echo '</form>'; echo '<p>Today is ', date ('l'), '. The current time is ', date ('g:i a'), '.</p>';
This final string will print<p>Today is Tuesday. The current time is 11:14 pm.</p>
using the appropriate formatting parameters for the date() function. - Complete the page.
include ('./footer.inc'); ?>
- Save the file as dateform.php, upload to your Web server, and test in your Web browser (
Figure 3.24
).
Figure 3.24 The date() and getdate() functions help add some dynamic behavior to this page.
Example 3.16. This script makes use of the date() and getdate() functions.
1 <?php # Script 3.16 - dateform.php 2 3 // Set the page title and include the HTML header. 4 $page_title = 'Calendar Form'; 5 include ('./header.inc'); 6 7 // This function makes three pull- down menus for the months, days, and years. 8 function make_calendar_pulldown ($this_month = NULL, $today = NULL, $year = NULL) { 9 10 // Make the months array. 11 $months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'); 12 13 // Make the pull- down menus. 14 echo '<select name="month">'; 15 foreach ($months as $key => $value) { 16 echo "<option value=\"$key\""; 17 if ($key == $this_month) { 18 echo ' selected="selected"'; 19 } 20 echo ">$value</option>\n"; 21 } 22 echo '</select> 23 <select name="day">'; 24 for ($day = 1; $day <= 31; $day++) { 25 echo "<option value=\"$day\""; 26 if ($day == $today) { 27 echo ' selected="selected"'; 28 } 29 echo ">$day</option>\n"; 30 } 31 echo '</select> 32 <select name="year">'; 33 34 if (!isset($year)) { 35 $year = date('Y'); 36 } 37 while ($year <= 2010) { 38 echo "<option value=\"$year\">$year</option>\n"; 39 $year++; 40 } 41 echo '</select>'; 42 } // End of the make_calendar_pulldown() function. 43 44 echo '<form action="dateform.php" method="post">'; // Create the form. 45 46 $dates = getdate(); 47 make_calendar_pulldown ($dates['month'], $dates['mday'], $dates['year']); // Make the calendar. 48 echo '</form>'; // End of form. 49 50 echo '<p>Today is ', date ('l'), '. The current time is ', date ('g:i a'), '.</p>'; 51 52 include ('./footer.inc'); // Include the HTML footer. 53 ?>