Jump to content


Photo

Date and Time Overview


  • Please log in to reply
No replies to this topic

#1 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 09 June 2006 - 01:16 PM

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:

DATE:     YYYY-MM-DD
TIME:     HH:MM:SS
DATETIME: YYYY-MM-DD HH:MM:SS

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 UNIX epoch 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\"]time()[/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\"]mktime()[/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\"]strtotime[/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\"]strtotime[/a]:

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

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\"]date()[/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\"]date()[/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:

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

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!
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users