PHP tutorial: Displaying Date and Time |
Code | Output | |
<?php print date("a"); ?> | pm | "am" or "pm" |
<?php print date("A"); ?> | PM | "AM" or "PM" |
<?php print date("d"); ?> | 15 | Day of the month: 01 to 31 |
<?php print date("D"); ?> | Tue | Day of the week: Sun, Mon, Tue, Wed, Thu, Fri, Sat |
<?php print date("F"); ?> | October | Month: January, February, March... |
<?php print date("h"); ?> | 03 | Hour: 01 to 12 |
<?php print date("H"); ?> | 15 | Hour: 00 to 23 |
<?php print date("g"); ?> | 3 | Hour: 1 to 12 |
<?php print date("G"); ?> | 15 | Hour: 0 to 23 |
<?php print date("i"); ?> | 26 | Minutes: 00 to 59 |
<?php print date("j"); ?> | 15 | Day of the month: 1 to 31 |
<?php print date("l"); ?> | Tuesday | Day of the week: Sunday, Monday, Tuesday... |
<?php print date("L"); ?> | 0 | Is it a leap year? 1 (yes) or 0 (no) |
<?php print date("m"); ?> | 10 | Month: 01 to 12 |
<?php print date("n"); ?> | 10 | Month: 1 to 12 |
<?php print date("M"); ?> | Oct | Month: Jan, Feb, Mar, Apr, May... |
<?php print date("s"); ?> | 03 | Seconds: 00 to 59 |
<?php print date("S"); ?> | th | Ordinal: 1st, 2st, 3st, 4st... Need to be used with a numeric time/date value. See latter. |
<?php print date("t"); ?> | 31 | Number of days in the month: 28 to 31 |
<?php print date("U"); ?> | 1034691963 | Seconds since 1970/01/01 00:00:00 |
<?php print date("w"); ?> | 2 | Day of the week: 0 (Sunday) to 6 (Saturday) |
<?php print date("Y"); ?> | 2002 | Year (four digits) |
<?php print date("y"); ?> | 02 | Year (two digits) |
<?php print date("z"); ?> | 287 | Day of the year: 0 to 365 |
<?php print date("Z"); ?> | -21600 | Difference in seconds from Greenwhich meridian |
The code |
Output |
<?php print date("Y"); ?>:<?php print date("m"); ?>: <?php print date("d"); ?> | 2002:10:15 |
<?php print
date("Y").":".date("m").":".date("d");
?> |
2002:10:15 |
<?php print
date("Y:m:d"); ?> |
2002:10:15 |
Y : m : d |
Year (four digits) no meaning Month: 01 to 12 no meaning Day of the month: 01 to 31 |
The code |
Output |
<?php print date("Y:m:d H:i") ?> | 2002:10:15 15:26 |
<?php print
date("l dS of F Y h:i:s A"); ?> |
Tuesday 15th of October 2002 15:26:03 PM |
The time is <?php print
date("H:i") ?>. That means it's <?php print date("i") ?> minutes past <?php print date("H") ?> o'clock. |
The time is 15:26. That means it's 26 minutes past 15 o'clock. |
The code |
Output |
Character with meaning |
<?php print date("Today is l"); ?> | WETo15pm02 2603 Tuesday | The following characters have a meaning: T, d,
a,
y, i, s, l |
<?php print
"Today is ".date("l"); ?> |
Today is Tuesday | Only data asociated to "l" (day of the week) is
requested |
To make the link, copy the code bellow to your page
Day |
Code |
Output |
2002/01/01 | 1st of January |
|
2002/01/02 |
2nd of January |
|
2002/01/03 |
3rd of January |
|
2002/01/04 |
4th of January |
In this tutorial we will consider our server is located in a different time zone from the one our clients are located at (a Belgium related site is in a server located is USA for example).
First we must know the time in the server. We will create a text file with the code bellow, and we will copy it to our server:
Time in server: <?php print date("H:i") ?> |
Then we will visit our page and we will get the time in the server. Let suppose the time is 16:00
Second, we will calculate the difference in hours between local time
and
the time in server. Let suppose the time in Belgium is 20:00, so the
difference
is 4 hours.
<?php $differencetolocaltime=4; $new_U=date("U")-$differencetolocaltime*3600; print date("H:i", $new_U); ?> |