Search
Tweets
Recent Changes
« Wow | Main | Officially Divorced »
Wednesday
Nov102004

A Gem For You

While spelunking in the depths of some code at work today, Jaxn found a little gem left by the previous explorers:

($day) = $dbh->selectrow_array("SELECT DATE_SUB(NOW(), INTERVAL 1 DAY)");
($day) = split(/s/, $day);

The only justification I have for this abomination is a situation where the boxes times are not synchronized. This method would force all machines to be synced from the time on the database server, but, cmon, we have ways of doing that.

For those of you playing along at home, the much less expensive method is:

@date = localtime(time() - 86400);
$date = sprintf("%d-%02d-%02d", $date[5] + 1900, $date[4] + 1, $date[3]);


I would use DateTime rather than time() or localtime().

UPDATE:

I found a similar line while I was doing my latrine duty:

my ($time) = $dbh->selectrow_array("SELECT NOW()");
$time = str_to_unixtime($time);


The more educated among you may recognize this as a reimplemention of time().

Reader Comments (1)

Code Snippet of the Week

# If we don't get a day , then use today's date.
#
if ( !$day ) {
($day) = $dbh->selectrow_array("SELECT DATE_SUB(NOW(), INTERVAL 1 DAY)");
($day) = split(/\s/, $day);
}

It is getting yesterday's date from the database instead of jus...

November 10, 2004 | Unregistered CommenterJackson Miller

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>