Wednesday, July 14

PHP: Dates and time in PHP

I will discuss dates and time in PHP. 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:14

No comments:

Post a Comment

Pencarian Google