The Cult of Leif Everything Leif M. Wright

3May/100

Date fun

I found early on, back when I was programming with Perl, that dealing with dates is a pain in the ass.

It has to do largely with our calendar system, which is screwed royally because of leap years, short months, etc.

So when I needed to detect if one item was older than the other, I came across the idea that server time doesn't really care what day it is, it just cares how much time has elapsed since a certain date.

That said, the rest was easy (this code is in php):

function dateIsCurrent($expiration_date){
if( strtotime($expiration_date) > time() ){
return true;
} else {
return false;
}
}

The function requires one argument (a date). strtotime is a PHP function that converts a supplied date into time, which is essentially a number of seconds. the function compares that time with the current time, and if it's greater than the current time, it returns true, meaning the supplied date is current. Otherwise, it returns false, because the current time is greater than the supplied time (meaning more seconds have passed in current time than in the time of the date that is supplied - or it's later now than it was in the date you supplied this function). Enjoy.

The way to call it is also simple:

if ($dateIsCurrent(2010-05-03) {
//do something
}

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.