Home and Learn: PHP Programming Course

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:


$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:


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.

Move on to the Next Part -->

Back to the PHP Contents Page


Email us: enquiry at homeandlearn.co.uk