This post covers the basic date & time operations which most of us are using in PHP and MySQL. The manipulation of the date and date & time fields in PHP is difficult because PHP is not very rich in date & time functions. This is why in most situations we have to write additional functions to subtract dates, calculate time intervals, calculate the difference between datetime elements and the list goes on.
Unlike PHP, MySQL is very rich in functions and supports for datetime elements. Its very good that we can always find what we need but this comes with a price: having so many options make the decision difficult.
In PHP there are 4 functions which can be used to handle most of the datetime operations:
- time() – returns the current time measured in the number of seconds since January 1 1970 00:00:00 GMT.
- mktime($hour,$minute,$second,$month,$day,$year) - returns the time specified through arguments measured in the number of seconds since January 1 1970 00:00:00 GMT.
- date($format,[$timestamp]) – transforms from a time variable produced by time or mktime into a formated string
- strtotime - transforms from a string to a time variable.
Lets start this tutorial creating an mysql table containing a DATETIME field: Read the rest of this entry »