PHP Time and Date
by Varun ThirayanAPDZ.com
Thursday, 9th March 2006
Dates
Dates are very simple in PHP and are easy to get the hang on but not so easy to remember all the variables to the dates as you will see further on in this tutorial.
In PHP time and dates, you use different letters to display different formats of the date as shown below. Remember that the time and date will be the one that the server is set to.
Please remember that the letters ARE case sensitive. So g and G are two different things.
<?php
// This will display something like June
// F simply means the full name of the month (January-December)
echo date("F");
?>
// This will display something like June
// F simply means the full name of the month (January-December)
echo date("F");
?>
If you wanted to display something a little longer than just the month, you can also input code such as below.
<?php
echo date("l dS of F Y");
?>
echo date("l dS of F Y");
?>
This will display something like Wednesday 19th of December 2005.
l = Full Day
d = Date
s = Suffix of the date (st, th)
F = Full Month
Y = Full year
d = Date
s = Suffix of the date (st, th)
F = Full Month
Y = Full year
As you may have noticed you can out real words in such as of and it will not affect the code but is advised to put most of the code outside the date tag.
Time
Time can also be displayed as below:
<?php
// Could display 07:25:56 PM
echo date("h:i:s A");
?>
// Could display 07:25:56 PM
echo date("h:i:s A");
?>
Extras - Variables
You also put date as a variable.
<?php
$time = date("l dS of F Y");
// Could display 07:25:56 PM
echo $time;
?>
$time = date("l dS of F Y");
// Could display 07:25:56 PM
echo $time;
?>
Find out more about variables by clicking here.
Options:
Printer Friendly
Email Friend
