The date() function is used to return the server time or a time from an optional time stamp.
You can format its output in different ways to suit your needs. We will see an example of the use of the time stamp later, but first lets see some simpler examples:
|
<?php print date("M-d-Y"); // prints something like Dec-01-2003;
print date("Today is D j of F"); // prints something like Today is Mon 1 of December;
print date("The year is y"); // prints something like The year is 03;
print date("The time is H:i:s"); // prints something like The time is 09:20:19; ?> |
Below is the complete table of format characters that you can use
Format | Description | Example |
a | Lowercase am or pm | am or pm |
A | Uppercase AM or PM | AM or PM |
B | Swatch Internet time | 000 through 999 |
d | Day of the month whith 2 digits | 01 to 31 |
D | Day of the week 3 letters | Mon, tue, etc. |
F | month | January, February, etc. |
g | 12 hour format without zeros | 1 to 12 |
G | 24 hour format without zeros | 1 to 23 |
h | 12 hour format with zeros | 01 to 12 |
H | 12 hour format with zeros | 01 to 23 |
i | Minutes with zeros | 00 to 59 |
I | Tells if the time is set to daylight saving time | 1 means true, 0 means false |
j | Day of the month | 1 to 31 |
l | Day of the week | Monday , Tuesday, etc. |
L | Tells if it's a leap year | 1 means true, 0 means false |
m | Month number with zeros | 01 to 12 |
M | Month in three letters | Jan, Feb, etc. |
n | Month number | 1 to 12 |
O | Difference to Greenwich time (GMT) | -0500 |
r | Date in RFC 822 format | Mon, 15 Jan 2003 13:45:32 -0500 |
s | Seconds with zeros | 01 to 59 |
S | English ordinal suffix for the day of the month, 2 letters | st, nd, rd, th |
t | Number of days in a month | 28 to 31 |
T | Timezone setting of the server | EST, MDT, etc |
U | Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) | 1054194954 |
w | Numeric representation of the weekday | 0 (sunday) to 6 (saturday) |
W | ISO-8601 week number of year | 35, 23, etc |
y | Year in four digits | 2003 |
Y | Year in two digits | 03 |
z | Day of the year in numbers | 0 to 366 |
Z | Timezone offset in seconds | -43200 to 43200 |
Since the date function returns the server time, if you want to return the user local time then you should make a function to modify the output. You can make that making use of the O format like in the following example:
|
<?php
$mygmt=-500; //this is the local time of the user
$timedif = date("O")-$mygmt; //this calculates the difference between the user local time and the server time
$mytime=date("U")-$timedif*3600/100;
print date("H:i", $mytime);
?> |
In the previous piece of code $mygmt is the user GMT time. You should take that input from the user. $timedif calculates the difference between the user local time and the server time. $mytime calculates a new unix time stamp to be use with the date function. This time stamp is the result of reducing the server time stamp date("U"), minus the user time difference with the server in seconds $timedif*3600/100. The last line just prints the date with the newly calculated time stamp.