PHP/Dates

PHP Dates and Times
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.

Date

 * 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:

Time

 * 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:

Mktime

 * 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 ). 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:


 * 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

 * 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:

Putting it All Together
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. \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!