Jump to content

Date and Time Overview


Recommended Posts

There is many a question raised when it comes to dates and times. With a basic understanding of the available functions and how they work, you can figure out a solution to nearly any situation. I will attempt to cover some of the basic principles and functions that apply in most situations here:

First off, you need to have an understanding of the difference between a mySQL DATE, TIME or DATETIME datatype and a PHP timestamp. As we know, these mySQL datatypes are stored in the following formats:

[code]
DATE:     YYYY-MM-DD
TIME:     HH:MM:SS
DATETIME: YYYY-MM-DD HH:MM:SS
[/code]

Now, to be able to do calculations on time with PHP, we need to have the date or time in the format of a UNIX timestamp, which is a count of the number of seconds that have passed since the [b]UNIX epoch[/b] at 12:00am on Jan 1, 1970.

There are a few functions we can look at that will create a UNIX timestamp for us. Let's look at those for a moment. First, you have the obvious [a href=\"http://www.php.net/time\" target=\"_blank\"][b]time()[/b][/a] function. This function takes no arguments, and it returns the current UNIX timestamp. Next we have [a href=\"http://www.php.net/mktime\" target=\"_blank\"][b]mktime()[/b][/a], which acts in much the same way, but it takes optional arguments of hour, minutes, seconds, month, day and year and returns the UNIX timestamp for that specified time and date.

Finally, and probably of most interest to some of you is [a href=\"http://www.php.net/strtotime\" target=\"_blank\"][b]strtotime[/b][/a]. This function takes one argument, which is a string representation of a date and/or time, and it returns the UNIX timestamp of that request. Why would this one be of interest? Well, simply for the fact that it translates mySQL date and time values into UNIX timestamps. So, to get a workable timestamp in PHP of the following date and time: "1980-02-12 05:45:36", I would simply need to run it through [a href=\"http://www.php.net/strtotime\" target=\"_blank\"][b]strtotime[/b][/a]:

[code]
<?php
echo strtotime("1980-02-12 05:45:36");
?>
[/code]

Now, some of you may be wondering why I went to all the trouble to write up an overview of these timestamp functions when there is the [a href=\"http://www.php.net/date\" target=\"_blank\"][b]date()[/b][/a] function that you've been wanting to get a grasp on. Well, that's because the [a href=\"http://www.php.net/date\" target=\"_blank\"][b]date()[/b][/a] function takes a UNIX timestamp as the second argument to properly display the result. So, before you can change the format of a MySQL DATETIME to display, you have to have it converted to a UNIX timestamp. So, let's follow our example above all the way through. Let's say I've just retrieved "1980-02-12 05:45:36" from my database, and I want to display that for my user in a friendly format. Well, I would simply need to do the following:

[code]
<?php
$val = strtotime("1980-02-12 05:45:36");
echo date("F j, Y @ g:ia", $val);
?>
[/code]

The code above would output "February 12, 1980 @ 5:45am". Simple once you get the handle on it, isn't it? Now keep in mind that the formatting can also be done using the SQL function DATE_FORMAT(), and there are also other date and time functions in PHP that are useful as well, but those will have to be covered in a more advanced section.

I hope this gives a good general overview and helps answer some of the repeated questions we receive about date and time conversions.

Good luck!
Link to comment
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.