PHP/Dates

< PHP
(Redirected from PHP Dates)

PHP Dates and Times

edit

One of the most challenging subjects for beginning coders to deal with in learning PHP can be accurately dealing with dates and times. Thankfully, this scripting language is equipped with a very complete set of functions to deal with date and time manipulation. This article will cover some of the basic functions and usage that PHP contains[1].

Functions

edit
The most basic, and quite possibly the most useful function at your disposal for manipulating dates is appropriately named "date." This function accepts one or two parameters. The first is a string value that represents the format in which you wish your date and/or time to be displayed. The variety of options available to be used can be found in the PHP manual. The second parameter is an optional UNIX timestamp that tells PHP what date and time you want to be displayed. If the second parameter is left empty, the date function will default to the current UNIX timestamp.
Example:
<?php
// Outputs 2 Jun 2007 - 9:07 am (or current date and time)
echo date('j M Y - g:i a');
?>
The time() function takes no parameters and simply returns the current UNIX timestamp. Without going into undue detail, remember that the UNIX timestamp is the number of seconds that have elapsed since the UNIX Epoch (Jan 1, 1970 00:00:00 GMT). With this definition, we can easily see how to select times and dates exact increments from our present time.
Example:
<?php
// Add the number of seconds in 24 hours
$tomorrow = time() + (60 * 60 * 24);
?>
While the time() function allows us to calculate quite easily based on the current UNIX timestamp, mktime() allows us to specify an exact moment in time (with some limitations[2]). The function accepts 0-7 optional parameters. Leaving all parameters off of the function call causes the same value to be returned as that of the time() function. However, the breakdown and flexibility of this function can be clearly seen as we look at the parameters.
  1. Hour – 0 through 24
  2. Minute – 0 through 60
  3. Second – 0 through 60
  4. Month – 0 through 12
  5. Day – 1 through the number of days in the month selected
  6. Year – 1970 through 2038 (view limitations)
  7. DST (Daylight Savings Time) – 0 for no, 1 for yes, or -1 for unsure
The ranges of values I present here are not actually limitations, but rather references for you to see how the function is intended to work. You may present any valid integer to any of the parameters, and mktime() will attempt to calculate the proper UNIX timestamp.
Example:
<?php
// Jan 22, 1994 1:00:00 PM
$time = mktime(13, 0, 0, 1, 22, 1994);

// Calculations are also allowed
// Jan 22, 1994 1:00:00 PM
$time = mktime(13, 0, 0, 13, 22, 1993);
?>
Notice in the second example that I reference the month of January 1994 by calling the 13th month of 1993 instead. Mktime() allows for such calculations, and because of this, we can come up with some very dynamic date and time calculations.
Strtotime() is a slightly different animal than mktime(), but it serves the same overarching purpose: it allows us to reference a specific date and/or time and gives us the UNIX timestamp for that time. The difference between the functions, though, is substantial. Strtotime() accepts nearly any human readable string as its first, required parameter. So, one could actually pass in something like January 1 or next Thursday to the function, and the appropriate timestamp is calculated. There is also a second, optional parameter that allows you to pass in the timestamp from which to calculate.
Example:
<?php
$time = strtotime("Jan 22, 1994 1:00 pm");

// Calculate from a specific timestamp, too
$ts = mktime(0, 0, 0, 1, 1, 1998);
$time = strtotime("next Monday", $ts);
?>

Putting it All Together

edit

After the basic functions we have reviewed, I'm sure you can see some incredible possibilities in date and time manipulation. If you are still struggling with joining these most basic of the date/time functions together, consider this final example in which we create a tabular calendar of the current month.

<?php
list($y, $m, $d) = explode('-', date('Y-m-d'));

$start  = mktime(0, 0, 0, $m, 1, $y);
$offset = 1 - date('w', $start);
$dayCnt = date('t', $start);

$month = date('F Y');
$weekdays = array('Su', 'M', 'Tu', 'W', 'Th', 'F', 'Sa');

echo "<table>\n";
echo "<tr><th colspan=\"7\">$month</th></tr>\n";
echo "<tr><td>" . implode('</td><td>', $weekdays) . "</td></tr>\n";

$x = 0;
for ($i = $offset; $i <= $dayCnt; $i++) {
	if ($x % 7 == 0) echo "<tr>\n";
	if ($i > 0) echo "<td>$i</td>\n";
	else echo "<td>&nbsp;</td>\n";
	$x++;
	if ($x % 7 == 0) echo "</tr>\n";
}
echo "</table>\n";
?>

There are so many other date and time functions in addition to these at your disposal, and I just don't have the time to cover them all. Study and choose what combination of functions works best for your situation, and you will be amazed at how powerful these functions can be.

Good luck!

References

edit
  1. http://www.php.net
  2. UNIX limitations include all dates before the UNIX Epoch as well as other known issues such as the Year 2038 problem.