Archive for August, 2008

Dates and Databases time() mktime() functions

Posted in Ramblings on August 2nd, 2008 by Jason – Be the first to comment

I recently was working on a website that was build using expression engine and the freeform module. I was creating a separate page that would query the entries table for free form and produce a csv file of all the entries for a certain time period. To do that I had to specify a date range.

I have worked on previous databases where the date is stored as.. a date, with year day and month. You can easily build your mysql queries if the entries are stored in the database using the date format. In expression engine it is a long integer that is a Unix times tamp. So what is that? The unix time stamp counts the number of seconds that have passed since January 1st 1970.

This time stamp can make it easier to compare dates, because you get ride of months, days, and years. No more leap years to worry about since it is all in seconds. The only thing you need to know is how to convert between the two. Luckily for us, php has already done the work and provided us with a couple functions to do the job, time() and mktime().

First of all, lets say you want to get todays date converted to the Unix Time Stamp.

echo time(); This returns the current Unix Time Stamp.

So when you enter something into the database, you can store the unix time, making it easier to do date searches. Now say you are retrieving the date from the database and want to display when your post was created. You obviously dont want to display the unix time stamp, unless you draw that kind of a crowd, most of your website visitors will not understand what it is, and for the people that do, they wont want to spend the time to convert it. This is where the date() function comes into play.

The date function takes in two variables, first is the formatting variable, how you want your date to appear, the second is a unix time stamp to specify the date you want. If the second variable is left blank, it defaults to the current time stamp, which gives you the current date.

;; Lets say that $time is the unix time stamp
;; for a specific day
date('d-m-Y', $time);
;; This returns the value of $time as day-month-year

For my project I had to do a little more, I needed to build my unix time from a specific date to be able to give me a range of time. For this I used the mktime() function. This function takes in several values: hour, minute, seconds, month, day, year, and if it is daylight savings time. All the values are not required, but if you are not going to enter all the values then you must leave them our from right to left. So if you want to skip the seconds value, just put in a zero, if you leave it out all together then it will think that the minutes is the seconds, the hours the minutes and so forth.

;; Lets say I want the unix time for 01-25-2008
mktime(0,0,0,1,25,2008); notice the zeros
;; How about Today at 11:45 and 23 seconds?
mktime(11,45,23);
;; notice I left the m/d/y blank since it defaults to current values

Post to Twitter Tweet This Post