Home and Learn: PHP Programming Course


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 January 2017)

<?PHP

$today = date('l jS F Y');
print $today;

?>

Example 2 (prints out something like "It's week 4 of 2017")

<?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().

<-- Back One Page | Move on to the Next Part -->

Back to the PHP Contents Page

 

Email us: enquiry at homeandlearn.co.uk