BigToach.com

PHP Snippets

« 1 2

age_days

Information

(PHP Version >= 3.0.6)
Functions used in this snippet: str_replace, date, substr, strlen.

Description

int age_days( mixed age[, mixed now] )

Takes a date and translates it into how many days it has been since the current time. If the optional now parameter is set, the age will be in days since now.

The date must be formatted in one of the following ways.
01/05/83
010583
01051983
01-05-83
01 05 1983
01,05,83

The year can either be 2 or 4 digits, but it must go month then year and each must be 2 digits long.

Snippet

<?php
function age_days($age$now=NULL){
    
$time = array();
    
$age str_replace(array('-''\\''/'' '','), ''$age);
    if(
$now === NULL){
        
$time['cd'] = date('d');
        
$time['cm'] = date('m');
        
$time['cy'] = date('Y');
    }else{
        
$now str_replace(array('-''\\''/'' '','), ''$now);
        
$time['cm'] = substr($now02);
        
$time['cd'] = substr($now22);
        
$time['cy'] = substr($now4);
        
$time['cy'] = strlen($time['cy']) > $time['cy'] : ($time['cy'] > date('y') ? '19' $time['cy'] : '20' $time['cy']);
    }
    
$time['bm'] = substr($age02);
    
$time['bd'] = substr($age22);
    
$time['by'] = substr($age4);
    
$time['by'] = strlen($time['by']) > $time['by'] : ($time['by'] > date('y') ? '19' $time['by'] : '20' $time['by']);
    
$time['fd'] = $time['cd'] - $time['bd'];
    if(
$time['fd'] < 0){
        
$time['fd'] = 31 $time['fd'];
        
$time['cm']--;
    }
    
$time['fm'] = $time['cm'] - $time['bm'];
    if(
$time['fm'] < 0){
        
$time['fm'] = 12 $time['fm'];
        
$time['cy']--;
    }
    
$time['fy'] = $time['cy'] - $time['by'];
    return ((
$time['fy'] * 365) + ($time['fm'] * 31) + $time['fd']);
}
?>

Example

age_days Can be used in the following way:

<?php
echo age_days('110583'); // age in days (as of 07/13/05 it is 7921)

echo age_days('11-05-1983''07-13-2005'); // 7921


?>

Added on Jul 13th at 7 am by Scott - 2 Comments


rating

Information

(PHP Version >= 4.3.0)
Functions used in this snippet: file_exists, is_readable, is_array, unserialize, file_get_contents, array_key_exists, array_sum, sizeof, array_merge, fopen, fwrite, serialize, fclose.

Description

str rating( str file, str get [, mixed add ] )

Uses file to store ratings for different values. The values are stored by their get key. If the add parameter is set, a new value will be added to get with the value of add.

Snippet

<?php
function rating($file,$get,$add=NULL){
    global 
$_array;
    if(!
file_exists($file) || !is_readable($file)){
        return 
0;
    }
    
$_array is_array($_array) ? $_array unserialize(file_get_contents($file));
    
$_array is_array($_array) ? $_array : array();
    if(
$add === NULL){
        if(
array_key_exists($get$_array)){
             
$rating array_sum($_array[$get]) / sizeof($_array[$get]);
        }else{
            return 
0;
        }
    }else{
        
$add is_array($add) ? $add : array($add);
        
$_array[$get] = array_merge($_array[$get],$add);
        
$rating array_sum($_array[$get]) / sizeof($_array);
        
$fp fopen($file,'w');
        
fwrite($fp,serialize($_array));
        
fclose($fp);
    }
    return 
$rating;
}
?>

Example

rating Can be used in the following way:

<?php
// add a vote of 5 to something with an id of 4
echo rating('file.txt''4''5'); // returns the average (5 if not previously set)

// add another vote
echo rating('file.txt''4''1'); // returns the average (3 using the example from above again (5+1/2))

// get the rating of something
echo rating('file.txt','4');
// returns the rating (using example from above 3 would return again);

echo rating('file.txt''newpost');
// gets the rating for the field stored as newpost
?>

Added on Jul 5th at 6 am by Scott - 0 Comments
Updated on Jul 5th at 6 am.


get_world_pop

Information

(PHP Version >= 3.0.12)
Functions used in this snippet: date, strtotime, time, number_format.

Description

str get_world_pop( [int seconds[, bool english]] )

calling get_world_pop with no arguements will return a formatted string of the estimated current world population at that second. If you supply the seconds parameter it will supply the world population for the date as long as it is between the years of 2005 and 2048. If the english value is set to false the formatted string will have periods instead of commas seperating thousands and hundreds etc.

Snippet

<?php
function get_world_pop($seconds=NULL,$english=true){
    
$pops = array(
            
"2005" => array( "start" => 6451058790"added" => 74427813),
            
"2006" => array( "start" => 6525486603"added" => 74629207),
            
"2007" => array( "start" => 6600115810"added" => 74940532),
            
"2008" => array( "start" => 6675056342"added" => 75228043),
            
"2009" => array( "start" => 6750284385"added" => 75466071),
            
"2010" => array( "start" => 6825750456"added" => 75688866),
            
"2011" => array( "start" => 6901439322"added" => 75802963),
            
"2012" => array( "start" => 6977242285"added" => 75615963),
            
"2013" => array( "start" => 7052858248"added" => 75167390),
            
"2014" => array( "start" => 7128025638"added" => 74490498),
            
"2015" => array( "start" => 7202516136"added" => 73766792),
            
"2016" => array( "start" => 7276282928"added" => 73055605),
            
"2017" => array( "start" => 7349338533"added" => 72230253),
            
"2018" => array( "start" => 7421568786"added" => 71289623),
            
"2019" => array( "start" => 7492858409"added" => 70235773),
            
"2020" => array( "start" => 7563094182"added" => 69180831),
            
"2021" => array( "start" => 7632275013"added" => 68144353),
            
"2022" => array( "start" => 7700419366"added" => 67028250),
            
"2023" => array( "start" => 7767447616"added" => 65862519),
            
"2024" => array( "start" => 7833310135"added" => 64679285),
            
"2025" => array( "start" => 7897989420"added" => 63596858),
            
"2026" => array( "start" => 7961586278"added" => 62635490),
            
"2027" => array( "start" => 8024221768"added" => 61689308),
            
"2028" => array( "start" => 8085911076"added" => 60746079),
            
"2029" => array( "start" => 8146657155"added" => 59800227),
            
"2030" => array( "start" => 8206457382"added" => 58925303),
            
"2031" => array( "start" => 8265382685"added" => 58132001),
            
"2032" => array( "start" => 8323514686"added" => 57333600),
            
"2033" => array( "start" => 8380848286"added" => 56512857),
            
"2034" => array( "start" => 8437361143"added" => 55666984),
            
"2035" => array( "start" => 8493028127"added" => 54846652),
            
"2036" => array( "start" => 8547874779"added" => 54058633),
            
"2037" => array( "start" => 8601933412"added" => 53249683),
            
"2038" => array( "start" => 8655183095"added" => 52414904),
            
"2039" => array( "start" => 8707597999"added" => 51542658),
            
"2040" => array( "start" => 8759140657"added" => 50686755),
            
"2041" => array( "start" => 8809827412"added" => 49847392),
            
"2042" => array( "start" => 8859674804"added" => 48957732),
            
"2043" => array( "start" => 8908632536"added" => 48019825),
            
"2044" => array( "start" => 8956652361"added" => 47040969),
            
"2045" => array( "start" => 9003693330"added" => 46074635),
            
"2046" => array( "start" => 9049767965"added" => 45123225),
            
"2047" => array( "start" => 9094891190"added" => 44148176),
            
"2048" => array( "start" => 9139039366"added" => 43161841));
            
    
$yearly 31535999// seconds in a year usually
    
    
if($seconds !== NULL){
        
$year date('Y',$seconds);    
        
$thisyear $seconds strtotime('jan 1 ' $year ' 00:00');
    }else{
        
$year date('Y');    
        
$thisyear time() - strtotime('jan 1 ' $year ' 00:00');
    }
    
$sep $english ',' '.';
    return 
number_format(($pops[$year]['start'] + (($thisyear $yearly) * $pops[$year]['added'])),0,'.',$sep);
}
?>

Example

get_world_pop Can be used in the following way:

<?php
echo get_world_pop(); 
// returns the current world population formatted like 6,578,968,451

echo get_world_pop(strtotime('July 6 2010'));
// returns the world estimated world populating on july 6 2010

echo get_world_pop(NULL,false); 
// returns the current world population formatted like 6.578.968.451
?>

Added on Jul 5th at 1 am by Scott - 0 Comments


get_month

Information

(PHP Version >= 4.05)
Functions used in this snippet: in_array, strtolower, array_search, strtotime.

Description

array get_month( str month, mixed year)

Takes a text month and numeric year and returns the unix timestamps for the very beginning of the month, and very end of the month.
Returns false on failure.

Snippet

<?php
function get_month($month,$year){
    
$months = array('january''february''march''april''may''june''july''august''september''october''november''december');
    
$ret = array();
    if(
in_array(strtolower($month), $months)){
        
$ret[] = strtotime($month ' 1, ' $year ' 00:00');
        
$nextmonth $months[array_search($month,$months)];
        
$year $nextmonth === 'january' $year++ : $year
        
$ret[] = strtotime($nextmonth ' 1, ' $year ' 00:00 -1 second');
        return 
$ret;
    }
    return 
false;
}
?>

Example

get_month Can be used in the following way:

<?php
$time 
get_month('january','2007');
echo 
$time[0] . ' ' $time[1]; // outputs "1167638400 1169798399"
$time get_month('july',2000);
echo 
date('r',$time[0]) . ' ' date('r',$time[1]);
// outputs "Sat, 1 Jul 2000 00:00:00 -0700 Tue, 25 Jul 2000 23:59:59 -0700"
?>

Added on Jun 26th at 6 am by Scott - 0 Comments
Updated on Jun 26th at 8 am.


timer

Information

(PHP Version >= 4.0.4)
Functions used in this snippet: array_sum, microtime, explode, number_format.

Description

float timer([float start,[int num]])

sets a start timer and outputs another timer less start to num decimals.

Snippet

<?php
function timer($start='',$num=8){
    if(
$start === ''){
        return 
array_sum(explode(' ',microtime()));
    }else{
        return 
number_format(timer() - $start,$num);
    }
}
?>

Example

timer Can be used in the following way:

<?php
$start 
timer();
// code to be timed goes here
echo timer($start); // shows elapsed time to 8 decimal places

$start timer();
// code to be timed goes here
echo timer($start,12); // shows elapsed time to 12 decimal places
?>

Added on Jun 23rd at 9 pm by Scott - 0 Comments
Updated on Jul 1st at 4 am.


« 1 2