Dates and time in PHP
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 time()
function, which displays the current time measured in the number of seconds
since the Unix Epoch (January 1 1970 00:00:00 GMT):
<? print time(); ?>
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:
1069142586
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 date() function.
This date function is used to convert from a UNIX timestamp to a human readable
date. The 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:14As 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:
| F | month, textual, long; e.g. "January" |
| H | hour, 24-hour format; i.e. "00" to "23" |
| i | minutes; i.e. "00" to "59" |
| j | day of the month without leading zeros; i.e. "1" to "31" |
| l (lowercase 'L') | day of the week, textual, long; e.g. "Friday" |
| s | seconds; i.e. "00" to "59" |
| S | English ordinal suffix for the day of the month, 2 characters; i.e. "st", "nd", "rd" or "th" |
| Y | 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:
| format character |
Description | Example returned values |
|---|---|---|
| a | Lowercase Ante meridiem and Post meridiem |
am or pm |
| A | Uppercase Ante meridiem and Post meridiem |
AM or PM |
| B | Swatch Internet time | 000 through 999 |
| d | Day of the month, 2 digits with leading zeros |
01 to 31 |
| D | A textual representation of a day, three letters |
Mon through Sun |
| F | A full textual representation of a month, such as January or March |
January through December |
| g | 12-hour format of an hour without leading zeros |
1 through 12 |
| G | 24-hour format of an hour without leading zeros |
0 through 23 |
| h | 12-hour format of an hour with leading zeros |
01 through 12 |
| H | 24-hour format of an hour with leading zeros |
00 through 23 |
| i | Minutes with leading zeros | 00 to 59 |
| I (capital i) | Whether or not the date is in daylights savings time |
1 if Daylight Savings Time, 0 otherwise. |
| j | 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 |
| L | Whether it's a leap year | 1 if it is a leap year, 0 otherwise. |
| m | Numeric representation of a month, with leading zeros |
01 through 12 |
| M | A short textual representation of a month, three letters |
Jan through Dec |
| n | Numeric representation of a month, without leading zeros |
1 through 12 |
| O | Difference to Greenwich time (GMT) in hours |
Example: +0200 |
| r | RFC 822 formatted date | Example: Thu, 21 Dec 2000 16:01:07 +0200 |
| s | Seconds, with leading zeros | 00 through 59 |
| S | English ordinal suffix for the day of the month, 2 characters |
st, nd, rd or th. Works well with j |
| t | Number of days in the given month | 28 through 31 |
| T | Timezone setting of this machine | Examples: EST, MDT ... |
| U | Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) |
See also time() |
| w | Numeric representation of the day of the week |
0 (for Sunday) through 6 (for Saturday) |
| W | ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0) |
Example: 42 (the 42nd week in the year) |
| Y | A full numeric representation of a year, 4 digits |
Examples: 1999 or 2003 |
| y | A two digit representation of a year | Examples: 99 or 03 |
| z | The day of the year | 0 through 366 |
| Z | 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 |
Source:
http://www.php.net/manual/en/function.date.php
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);
//Output: SundayWow, 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
keys are:
| seconds | seconds |
| minutes | minutes |
| hours | hours |
| mday | day of the month |
| wday | day of the week, numeric: from 0 as Sunday up to 6 as Saturday |
| mon | month, numeric |
| year | year, numeric |
| weekday | day of the week, textual, full; i.e. "Friday" |
| month | month, textual, full; i.e. "January" |
Example :
$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
it.
print $days[date("w")]; // display name of day with our own language
print $months[date("n")];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.
Author biography
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
