BigToach.com

PHP Snippets

1 ... « 2 3 4 5 6 » ... 10

gmail_size

Information

(PHP Version >= 3)
Functions used in this snippet: time, substr, microtime, sizeof.

Description

int gmail_size( void )

Shows the current size that Google offers for its Gmail storage.

Note: It will only work until August 2008, assuming that Google continues to up its storage by 100 megs each month.

Snippet

<?php
function gmail_size(){
    
$sizes = array(    array(11202012000002350),
            array(
11228796000002450),
            array(
11255580000002550),
            array(
11281500000002650),
            array(
11308320000002750),
            array(
11334240000002850),                array(11361024000002950),
            array(
11387808000003050),
            array(
11412000000003150),                array(11438784000003250),
            array(
11464668000003350),
            array(
11491452000003450),                array(11517372000003550),
            array(
11544156000003650),
            array(
11570940000003750),                array(11596860000003850),
            array(
11623680000003950),
            array(
11649600000004050),                array(11676384000004150),
            array(
11703168000004250),
            array(
11727360000004350),                array(11754144000004450),
            array(
11780028000004550),
            array(
11806812000004650),                array(11832732000004750),
            array(
11859516000004850),
            array(
11886300000004950),                array(11912220000005050),
            array(
11939040000005150),
            array(
11964960000005250),                array(11991744000005350),
            array(
12018528000005450),
            array(
12043584000005550),                array(12070368000005650),
            array(
12096252000005750),
            array(
12123036000005850),                array(12148956000005950),
            array(
12175740000006050),
            array(
12202524000006150));
    
$now time() . substr(microtime(), 23);
    for(
$i=0$i<sizeof($sizes); $i++){
        if(
$now $sizes[$i][0]){
            break;
        }
    }
    if(
$i == 0){
        
$ret $sizes[0][1];
    }elseif(
$i == sizeof($sizes)){
        
$ret $sizes[$i 1][1];
    }else{
        
$ts $sizes[$i 1][0];
        
$bs $sizes[$i 1][1];
        
$ret = (($now $ts) / ($sizes[$i][0] - $ts) * ($sizes[$i][1] - $bs)) + $bs;
    }
    return 
$ret;
}
?>

Example

gmail_size Can be used in the following way:

<?php
echo gmail_size(); // returns 2479.784683

// it will be different every time you see it

?>

Added on Aug 10th at 8 am by Scott - 0 Comments


letterfy

Information

(PHP Version >= 4)
Functions used in this snippet: is_numeric, strlen, in_array, substr.

Description

str letterfy( int int )

Returns a [typestr[/type] suffix of either st, nd, rd, or th based on the last 1 or two digits of the supplied int

Snippet

<?php
function letterfy($int){
    if(!
is_numeric($int{strlen($int) - 1})) return;
    if(
in_array(substr($int,-2), array(11,12,13))) return 'th';
    switch(
$int{strlen($int) - 1}){
        case 
1: return 'st';
        case 
2: return 'nd';
        case 
3: return 'rd';
        default: return 
'th';
    }
}
?>

Example

letterfy Can be used in the following way:

<?php
$x 
10;
echo 
$x letterfy($x); // 10th

$x 1;
echo 
$x letterfy($x// 1st

$x 22;
echo 
$x letterfy($x// 22nd

$x 33;
echo 
$x letterfy($x// 33rd

$x 111;
echo 
$x letterfy($x// 111th


?>

Added on Aug 10th at 7 am by Scott - 0 Comments


array_sum_assoc

Information

(PHP Version >= 3)
Functions used in this snippet: is_array, sizeof, array_sum_assoc.

Description

int array_sum_assoc( array array )

Sum's up all the values in the array array associatively.

Snippet

<?php
function array_sum_assoc($array){
    if(!
is_array($array) || sizeof($array) === 0){
        return 
false;
    }
    
$count 0;
    foreach(
$array as $key=>$value){
        if(
is_array($value)){
            
$count += array_sum_assoc($value);
        }else{
            
$count += $value;
        }
    }
    return 
$count;
}
?>

Example

array_sum_assoc Can be used in the following way:

<?php
// make an associative array
$array = array(1,array(1),array(1,array(1)));

echo 
array_sum_assoc($array);
// outputs 4, array_sum would output 1

?>

Added on Aug 9th at 7 pm by Scott - 0 Comments


avg

Information

(PHP Version >= 4.0.4)
Functions used in this snippet: is_array, sizeof, array_sum.

Description

int avg( array array )

Takes the array array and gives you the average of its values.

Snippet

<?php
function avg($array){
    if(
is_array($array) && sizeof($array) !== 0){
        return (
array_sum($array) / sizeof($array));
    }
    return 
false;
}
?>

Example

avg Can be used in the following way:

<?php
$array 
= array(1,3,4,9,41,5,87,6,62,4,8,6,8,7,5,6);

echo 
avg($array); // 16.375
?>

Added on Aug 8th at 5 am by Scott - 0 Comments


time_since

Information

(PHP Version >= 3)
Functions used in this snippet: time, date, ceil, floor.

Description

str time_since( int time[, int now[, str fmt]] )

Returns the textual difference between times. If the optional now is left as NULL, the current time will be used, otherwise the time given will be used. If the optional fmt is set, the time that is returned will be formatted according to this string (assuming it is older than 2 days old, Reference www.php.net/date for how to format this).

Snippet

<?php
function time_since($time$now=NULL$fmt='l F jS, g:i a'){
    if(
$now === NULL){
        
$now time();
    }
    
$diff $now $time;
    
$today date('dmy'$now) === date('dmy'$time) ? true false;
    if(
$today && $diff 60 60){
        
$num ceil($diff 60);
        return 
$num ' minute' . ($num 's' '') . ' ago';
    }elseif(
$today){
        
$num floor($diff 60 60);
        return 
$num ' hour' . ($num 's' '') . ' ago';
    }else{
        
$thisyear date('y'$now) === date('y'$time) ? true false;
        
$daydiff date('z'$now) - date('z'$time);
        if(
$daydiff === && $thisyear){
            return 
'Yesterday';
        }
    }
    return 
date($fmt$time);
}
?>

Example

time_since Can be used in the following way:

<?php
echo time_since(time());
// returns something like 20 minutes ago

echo time_since('1121913694''1121920868');
// returns 1 hour ago

echo time_since(strtotime('yesterday'));
// returns Yesterday

echo time_since(strtotime('-2 months'),NULL'm/d/y');
// returns something like 18/05/05
?>

Added on Jul 21st at 1 am by Scott - 3 Comments
Updated on Nov 4th at 4 am.


1 ... « 2 3 4 5 6 » ... 10