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
}

Filed under: PHP, Programming No Comments
3May/100

Detecting browser in php for css

In designing websites, I've discovered that the old problems with Internet Explorer are still around, mainly that you can code a beautiful website only to discover that Internet Explorer doesn't support the coolest things you've done.

The reasons behind this are multiple, but the short version is that Microsoft wanted to own the Internet, so it refused to participate in Web standards by having their browser support them, forcing webpage designers to make concessions for Internet Explorer when they designed. The hope was that they'd just throw up their hands, use Internet Explorer's proprietary coding and eventually everyone would have to use Internet Explorer (and Windows) if they wanted to surf the Web.

But that backfired, and Internet Explorer is losing Web presence daily. That said, the newest Web standards are unevenly implemented across browsers, with Safari being the only browser I've found so far that actually implements CSS3 properly. The problem is, JavaScripts designed to detect browsers are woefully out of date and oftentimes provide wrong information, meaning you have to put in a boatload of effort to code a page to detect which browser you're using, and then it's tough to implement whatever changes you make throughout the page.

This stuck out like a sore thumb on one of the latest sites I was coding, Square Deal Music, for which I had coded all sorts of cool stuff, including gradient overlays in the div containers, which were not supported on Firefox, I discovered to my chagrin. That means Firefox users would see my dark text against a dark background, making it quite difficult to read - and ugly to boot.

So Friday I came up with a solution: I would code one CSS stylesheet for Safari (which adheres completely to Web standards) and another for every other browser. Then, in php, I would detect the browser and print the link for the correct stylesheet based on which browser was being used:

if ( strpos($_SERVER['HTTP_USER_AGENT'], 'Safari') ){
$Safari = TRUE;
echo "<link rel=\"stylesheet\" href=\"Safari.css\">";
} else {
$Safari = FALSE;
echo "<link rel=\"stylesheet\" href=\"BadBrowser.css\">";
}

strpos returns the position of the quoted string in the passed string. If the string does not exist inside the larger string, it returns FALSE, which is why I use it here as kind of a poor man's grep or regex. I'm just testing to see if the string "Safari" exists in the user-agent field the HTTP server passes to php.

A bonus of this approach is the $Safari variable is then available through the rest of the page so I can implement all sorts of things, like popping up a div to alert the viewer that they can download a free browser to see the site as it was intended. Like this:

if ($Safari == FALSE) {
echo "<div class=\"badbrowser\"><h2>BAD BROWSER! You are viewing a crippled version of this site because your browser is not standards-compliant. Download <a href=http://apple.com/safari>Safari</a> for free and you can see this site as it was intended.</h2></div>";
}

Fun, eh?