PHP Chapter 11 (Date and Time Functions in PHP)

The PHP date function

Knowing how to handle date and time values in PHP will be a useful addition to your programming skills. In this and the following sections, we'll take a look at how to process this type of data.

The date( ) function

The inbuilt PHP function date( ) is the most widely used method of returning date values. Unfortunately, there is a very long list of things you can put between the round brackets of the function! Try this script, to get an idea of how it works:
<?php
$today = date('d-m-y');
print $today;
?>
It should print the day of the week first (d), then the month (m), then the year (y). But this will be the numerical format. So it will print something like:
04-07-2006
This type of date can be very confusing, however, because it means the 7th of April in the USA. In the UK, it means the 4th of July.
But to use the function, you first type date followed by the round brackets. In between the round brackets you can type a whole host of different date combinations. There's a list coming up. But take note of the case. Change your script to capital letters and watch what happens.
Also, the separator can be anything you like (within reason). So you can have this instead of a hyphen:
$today = date('d:m:y');
Or this:
$today = date('d m y');
Or even this:
$today = date('d~m~y');
Note the single quote marks surrounding the date text. Miss these out and you'll get errors. You can use double quotes, but singles are recommended: dates can be a bit quirky.
Click the next part for a fuller list of the date and time characters to use between the round brackets of date.

Using the PHP date function

The following is some lists of the date and time characters to use between the round brackets of date. They are all case sensitive. The date function syntax is this, remember:
date( date_characters_here )
The script from the previous page was this:
<?php
$today = date('d-m-y');
print $today;
?>
The first list is for the day of the week characters. Try them out on your script above.
Day of the week Characters (opens in a new window)
You'll also need the year characters:
Year Characters (opens in a new window)
To add the Time, use the following characters:
Time Characters (opens in a new window)
Here's some other Characters that may come in handy:
Other Characters (opens in a new window)
That's quite a lot of characters! Mostly, you'll be dipping in and out to find the one you need. Here's a few examples of the way you can use the above. Try out the following scripts to see how they work.
Example 1 (prints out something like Monday 7th September 2006)
<?PHP
$today = date('l jS F Y');
print $today;
?>
Example 2 (prints out something like "It's week 9 of 2006")
<?PHP
$today = date('W');
$year = date('Y');
print "It's week " . $today . " of " . $year;
?>
Example 3 (prints out something like "11:25:44 am")
<?PHP
$time = date('h:i:s a');
print $time;
?>

Example 4 (prints out something like "23:28 GMT Standard Time")
<?PHP
$time = date('G:i T');
print $time;
?>
In the next part, we'll see another useful date/time function when we look at getdate().

The PHP getdate Function

Another useful date/time function is getdate. This will return an array (associative) with all the date and time values. You can use it for things like comparing one date to another. For example, comparing how many days have passed since a given date. Here's the syntax:
getdate( time_stamp );
The time stamp is optional. If you leave it out, it gets the values for the current local date and time. The parts of the array are this:
seconds
minutes
hours
mday (day of the month as a number)
wday (day of the week as a number)
mon (month a number)
year
yday (year day as a number)
weekday (day in text format)
month (month in text format)
0 (Seconds since the Unix Epoch)
Because getdate returns an associative array, you can just do this sort of thing:
$today = getdate();
print $today['mday'];
print $today['wday'];
print $today['yday'];
So whichever part of the array you want to access goes between square brackets. You then type one of the above Keys between quote marks.
As a further example, suppose you want to work out how many days it's been since a forum member last posted something. And that you have used this to write the date of the last post in a database:
$post_date = date('z');
If you look at the previous tables, you'll see that "z" means the year day as a number. So a value of 60 would mean the 60th day of the year.
Now, you've read this value back in, and you want to compare that date against today's date. You can do it like this:
<?PHP
$post_date = 60;
$today = getdate();
$day_difference = $today['yday'] - $post_date;
Print "Days since last post = " . $day_difference;
?>
So we've set up the array using getdate:
$today = getdate();
We've then used "yday" to calculate how many days have elapsed since the last post:
$day_difference = $today['yday'] - $post_date;

Working with dates and times can be quite tricky, and a good reference is the PHP.net website. As well as setting out all the date and time functions, there's lots of posts from people with good date/time scripts:
In the next section of the course, we'll explore databases.

Comments