In this tutorial I'll discuss dates and time in PHP, which is useful for a
myriad of tasks. To kick things off, take a look at PHP's
function, which displays the current time measured in the number of seconds
since the Unix Epoch (January 1 1970 00:00:00 GMT):
This is also called a UNIX timestamp, and as mentioned, displays the current
time in seconds passed since the Unix Epoch. A sample output may look like:
Using date() to display the date and time
Now, we want to display not just the current time and date, but in a user
friendly format. To do that we should enlist PHP's
This date function is used to convert from a UNIX timestamp to a human readable
date() function looks like:
string date ( string format [, int timestamp])
You can see that the timestamp is surrounded by '[' and ']', which means it's
optional. If we put it then the function will use it and if we don't put it then
the function will use the default timestamp. The default timestamp which is used
when we don't put any timestamp is the current time.
Let's start our work with the date function. We want to show the user what
date and time is now.
print date("l, F jS Y - H:i:s");
//Example output: Tuesday, November 18th 2003 - 03:20:14
As you can see on the example above, I use l, F jS Y - H:i:s as the variable
passed to the function. Here's a description of each of the formatting
characters used above:
||month, textual, long; e.g. "January"
||hour, 24-hour format; i.e. "00" to "23"
||minutes; i.e. "00" to "59"
||day of the month without leading zeros; i.e. "1" to
|l (lowercase 'L')
||day of the week, textual, long; e.g. "Friday"
||seconds; i.e. "00" to "59"
||English ordinal suffix for the day of the month, 2
characters; i.e. "st", "nd", "rd" or "th"
||year, 4 digits; e.g. "1999"
But wait, PHP actually supports many other characters you can pass into the
date function to format the output. Here's the complete list:
||Example returned values
||Lowercase Ante meridiem and Post
|am or pm
||Uppercase Ante meridiem and Post
|AM or PM
||Swatch Internet time
||000 through 999
||Day of the month, 2 digits with leading
|01 to 31
||A textual representation of a day,
|Mon through Sun
||A full textual representation of a
month, such as January or March
|January through December
||12-hour format of an hour without
|1 through 12
||24-hour format of an hour without
|0 through 23
||12-hour format of an hour with leading
|01 through 12
||24-hour format of an hour with leading
|00 through 23
||Minutes with leading zeros
||00 to 59
|I (capital i)
||Whether or not the date is in daylights
|1 if Daylight Savings Time, 0
||Day of the month without leading zeros
||1 to 31
|l (lowercase 'L')
||A full textual representation of the
day of the week
|Sunday through Saturday
||Whether it's a leap year
||1 if it is a leap year, 0 otherwise.
||Numeric representation of a month, with
|01 through 12
||A short textual representation of a
month, three letters
|Jan through Dec
||Numeric representation of a month,
without leading zeros
|1 through 12
||Difference to Greenwich time (GMT) in
||RFC 822 formatted date
||Example: Thu, 21 Dec 2000 16:01:07
||Seconds, with leading zeros
||00 through 59
||English ordinal suffix for the day of
the month, 2 characters
|st, nd, rd or th. Works well with j
||Number of days in the given month
||28 through 31
||Timezone setting of this machine
||Examples: EST, MDT ...
||Seconds since the Unix Epoch (January 1
1970 00:00:00 GMT)
||Numeric representation of the day of
|0 (for Sunday) through 6 (for Saturday)
||ISO-8601 week number of year, weeks
starting on Monday (added in PHP 4.1.0)
|Example: 42 (the 42nd week in the year)
||A full numeric representation of a
year, 4 digits
|Examples: 1999 or 2003
||A two digit representation of a year
||Examples: 99 or 03
||The day of the year
||0 through 366
||Timezone offset in seconds. The offset
for timezones west of UTC is always negative, and for those east of UTC
is always positive.
|-43200 through 43200
Using mktime() to manipulate past and future times
Another useful function in PHP is
mktime(), which can help you
easily display and format a past or future time (ie: what day of the week is
your birthday on)? This function creates a timestamp from a given set of
variables that you pass to the function.
mktime(hour, minute, second, month, date, year);
Now, I want to know on what day of the week my birthday was on. My birth date
is July 21st 1974. I'll use the mktime function like so:
$mybirthdate = mktime(0,0,0,7,21,1974);
print date("l", $mybirthdate);
Wow, I finally found out that my birthday is on Sunday. :). Now the next case
I want to know is what day is 25 days from now. Let's do it this way:
$next25day = mktime(0,0,0,date("n"),date("j")+25,date("Y"));
print date("l", $next25day);
Give attention to the variables that I passed to the mktime function. I've
passed date("j")+25. It means that I want to add 25 to the current date. You can
figure out what it will produce.
Using getdate() as an alternative to date()
This is another method to access the current date or the given timestamp. The
method, or you can call it function, is
getdate(). This function
will produce an associative array containing the date information. The array
||day of the month
||day of the week, numeric: from 0 as Sunday up to 6 as
||day of the week, textual, full; i.e. "Friday"
||month, textual, full; i.e. "January"
$today = getdate();
print $today["month"] // it will print the month of current date
print $today["weekday"] // it will print the day of current date
Displaying the date and time in your own local language
By this time I hope you can create your own date and time using the timestamp
of the current date or your own timestamp. I'm not using English as my primary
language, and neither are my website users. We are from Indonesia and I want to
display the date in Indonesian language. How can I do it? That's a perfect
question and asked by most beginners.
Let's create an associative array of date and month in our own language.
$days = Array ("Minggu", "Senin", "Selasa", "Rabu", "Kamis", "Jum'at", "Sabtu");
$months = Array (1=>"Januari", 2=>"Pebruari", 3=>"Maret", 4=>"April",
5=>"Mei", 6=>"Juni", 7=>"Juli", 8=>"Agustus", 9=>"September",
10=>"Oktober", 11=>"Nopember", 12=>"Desember");
After we create our own language of date and months, now it's time to display
print $days[date("w")]; // display name of day with our own language
Ok guys, that's it for today. We'll see you again next time.
Note: This tutorial has been modified by
CodeTricks.com for content and structure.
Hermawan Haryanto is web developer and offshore programmer. He's from Indonesia
and married. Hermawan is the senior developer at
Redrockreef.com and author of dEkap.com