PHP's date() function may seem to
have an overwhelming amount of options available.
it better to have more choices than not enough??
PHP's date function
you format timestamps, so they are more human readable.
Date - The
The date function always formats a
timestamp, whether you supply one or not. What's a timestamp? Good
A timestamp is the number of seconds from January 1, 1970 at 00:00.
Otherwise known as the Unix Timestamp, this measurement is a widely
used standard that PHP has chosen to utilize.
Date - What Time Is It?
date function uses letters of the alphabet to represent various parts
of a typical date and time format.?
letters we will be using in our
first example are:
The day of the month. The type of output you can expect is 01 through
- m: The current month, as a
number. You can expect 01 through 12.
The current year in two digits ##. You can expect 00 through 99
We will study the rest of the options later, but for now
let's use those above letters to format a simple date.
PHP uses to represent parts of date and time will automatically be
converted by PHP.
However, other characters like
a slash "/" can be inserted between the letters to add additional
We have opted to use the slash
in our example.
Supplying a Timestamp
As our first example shows, the first argument of the date
function tells PHP how you would like your date and time displayed. The
second argument allows for a timestamp and is optional.
example uses the mktime function to create a
timestamp for tomorrow. To go one day in the future we simply add one
to the day argument of mktime. For your future
reference, we have the arguments of mktime.
Note: These arguments are all optional. If
you do not supply any arguments the current time will be used to create
- mktime(hour, minute,
second, month, day, year, daylight savings time)
$tomorrow = mktime(0, 0, 0, date("m"), date("d")+1,
echo "Tomorrow is ".date("m/d/y",
Notice that we
used one letter at a time with the function date to
get the month, day and year. For example the date("m")
will return the month's number 01-12.
If we were to
run our new script just after the 2010 Winter Olympics our display
would look like:
Tomorrow is 02/28/2010
PHP Date - Reference
that you know the basics of using PHP's date
function, you can easily plug in any of the following letters to format
your timestamp to meet your needs.Important Full
Date and Time:
Displays the full date, time and timezone offset. It is equivalent to
manually entering date("D, d M Y H:i:s O")
- a: am or pm depending on the
- A: AM or PM depending on
- g: Hour without
leading zeroes. Values are 1 through 12.
Hour in 24-hour format without leading zeroes. Values are 0 through 23.
- h: Hour with leading zeroes.
Values 01 through 12.
- H: Hour
in 24-hour format with leading zeroes. Values 00 through 23.
Minute with leading zeroes. Values 00 through 59.
Seconds with leading zeroes. Values 00 through 59.
- d: Day of the month with
leading zeroes. Values are 01 through 31.
Day of the month without leading zeroes. Values 1 through 31
Day of the week abbreviations. Sun through Sat
Day of the week. Values Sunday through Saturday
Day of the week without leading zeroes. Values 0 through 6.
Day of the year without leading zeroes. Values 0 through 365.
- m: Month number with leading
zeroes. Values 01 through 12
Month number without leading zeroes. Values 1 through 12
Abbreviation for the month. Values Jan through Dec
Normal month representation. Values January through December.
The number of days in the month. Values 28 through 31.
- L: 1 if it's a leap year and 0
if it isn't.
- Y: A four digit
- y: A two digit
year format. Values 00 through 99.
- U: The
number of seconds since the Unix Epoch (January 1, 1970)
This represents the Timezone offset, which is the difference from
Greenwich Meridian Time (GMT). 100 = 1 hour, -600 = -6 hours
For more detail please see Php Manual at http://www.php.net