Playing With Dates

April 13th, 2009 | Tags:

Tutorial Description:

Learn how to play and use dates with PHP.

This tutorial will teach you the basics on how to use time & date with PHP.

Ever wondered how time works on the internet, very simple actually. All times are measured by the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

The measurement is called a timestamp, which contains the large date number.

You can get the timestamp of the current time with the following function:

<?php

$timestamp

= mktime();

?>

Now that we have the timestamp, we can transform it to our favorite format. For this, we’ll need to use the date() function.

Lets see an example:

<?php

$timestamp

= mktime();

// Lets show this format: day - month - year
echo date("d - m - Y", $timestamp);

?>

That was a very dull example, lets have some fun!

<?php

$timestamp

= mktime();

// Shows something like this: Today is Thursday, August 18th, 2005.
echo date("\T\o\d\a\y \i\s l, F jS, Y\.", $timestamp);

?>

As you can see above, if you want to use text within the date function, you must escape each character first with a “\”.

Thats about it, you can play with the formats even more, check out all the formats at PHP.net

No comments yet.
TOP