Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Natalie Downe http://natbat.co.uk/

« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS 

Timesince

<?php

/* Works out the time since the entry post, takes a an argument in unix time (seconds) */
function time_since($original) {
    // array of time period chunks
    $chunks = array(
        array(60 * 60 * 24 * 365 , 'year'),
        array(60 * 60 * 24 * 30 , 'month'),
        array(60 * 60 * 24 * 7, 'week'),
        array(60 * 60 * 24 , 'day'),
        array(60 * 60 , 'hour'),
        array(60 , 'minute'),
    );
    
    $today = time(); /* Current unix time  */
    $since = $today - $original;
    
    // $j saves performing the count function each time around the loop
    for ($i = 0, $j = count($chunks); $i < $j; $i++) {
        
        $seconds = $chunks[$i][0];
        $name = $chunks[$i][1];
        
        // finding the biggest chunk (if the chunk fits, break)
        if (($count = floor($since / $seconds)) != 0) {
            // DEBUG print "<!-- It's $name -->\n";
            break;
        }
    }
    
    $print = ($count == 1) ? '1 '.$name : "$count {$name}s";
    
    if ($i + 1 < $j) {
        // now getting the second item
        $seconds2 = $chunks[$i + 1][0];
        $name2 = $chunks[$i + 1][1];
        
        // add second item if it's greater than 0
        if (($count2 = floor(($since - ($seconds * $count)) / $seconds2)) != 0) {
            $print .= ($count2 == 1) ? ', 1 '.$name2 : ", $count2 {$name2}s";
        }
    }
    return $print;
}

?>

JS: fast through a list

var i = list.length;
for(i; i> 0; i--) {
	alert(i)
}

Object paternity

// object paternity in Javascript

// relationship of object 1 to object 2
function objectPaternity(obj1, obj2) {
 // given an object this function returns 'child' if obj1 is a child of obj2
 // and 'parent' if obj1 is a parent of obj2 - then 'no' if neither
 var relationship = 'no';
 var currentElement = obj1;
 
 while((typeof currentElement.parentNode != 'undefined') && (currentElement != document.body)) {
  // first cycle through all the parent elements of object 1 to see if object 2 is one of them
  if(currentElement.parentNode == obj2) {
   relationship = 'child';
  }
  // walk up the tree and try again
  currentElement = currentElement.parentNode;
 }
 // if object 1 is not a child of object 2 then we test the other way arround
 if(relationship == 'no') {
  currentElement = obj2;
  while((typeof currentElement.parentNode != 'undefined') && (currentElement != document.body)) {
   // now cycle through all the parent elements of object 2 to see if object 1 is one of them
   if(currentElement.parentNode == obj1) {
    relationship = 'parent';
   }
   // walk up the tree and try again
   currentElement = currentElement.parentNode;
  }
 }
 return relationship;
}

Current year

// outputs current year

#year(now())#.

Browser detect for IE versions

// is called when you want to test which version of IE a user has, returns true or false

function isIE(versionNumber) {
var detect = navigator.userAgent.toLowerCase();
if(!(navigator && navigator.userAgent && navigator.userAgent.toLowerCase)) {
  	        return false;
  	    } else {
  	        if(detect.indexOf('msie') + 1) {
  	            // browser is internet explorer
  	            var ver = function() {
  	                // http://msdn.microsoft.com/workshop/author/dhtml/overview/browserdetection.asp
  	                // Returns the version of Internet Explorer or a -1
  	                // (indicating the use of another browser).
  	                var rv = -1; // Return value assumes failure
  	                if (navigator.appName == 'Microsoft Internet Explorer') {
  	                    var ua = navigator.userAgent;
  	                    var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
  	                    if (re.exec(ua) != null) {
  	                        rv = parseFloat( RegExp.$1 );
  	                    }
  	                }
  	                return rv;
  	            };
  	            var valid = true;
  	            // if the version can be found and the version is less than our version number it is invalid
  	            if ((ver > -1) && (ver < versionNumber)) {
  	                valid = false;
  	            }
  	            return valid;
  	        } else {
  	            return false
  	        }
  	    }
} 

« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS