Like most programming languages, PHP comes with a fairlyfull-featured API for date and time value manipulation. You've probablyused it in your applications, but never bothered to look too closely atit. Well, here's your chance to rectify that mistake - this articledelves into the date/time API in depth, uncovering some hidden nuggetsand demonstrating how it can be used to simplify date and timeprocessing in your PHP scripts.
Most of PHP's date functions work on the basis of timestamps. This timestamp is a unique numeric representation of a particular date, calculated as the number of seconds between January 1 1970 and the date and time specified, and makes it easier to perform arbitrary calculations on date and time values.
In PHP, UNIX timestamps are created via the mktime() function, which accepts a series of date and time parameters, and converts them into a timestamp. Here's an example:
// get a timestamp for 14:35:20 April 1 2002
echo mktime(14, 35, 20, 4, 1, 2002);
In this case, I've passed the mktime() function six
parameters: the hour, minute, second, month, day and year for which I need a timestamp. The result is a long integer like this: 1017651920This integer can now be passed to getdate() - or any other date function that accepts timestamps - and converted into a human-readable representation.
You can obtain a timestamp for the current moment in time by calling mktime() with no arguments:
// get a timestamp for now
Notice also that the "0" key of the array returned by
getdate() contains a UNIX timestamp representation of the date returned.