PHP Snippets
1 ... « 2 3 4 5 6 » ... 10gmail_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(1120201200000, 2350),
array(1122879600000, 2450),
array(1125558000000, 2550),
array(1128150000000, 2650),
array(1130832000000, 2750),
array(1133424000000, 2850), array(1136102400000, 2950),
array(1138780800000, 3050),
array(1141200000000, 3150), array(1143878400000, 3250),
array(1146466800000, 3350),
array(1149145200000, 3450), array(1151737200000, 3550),
array(1154415600000, 3650),
array(1157094000000, 3750), array(1159686000000, 3850),
array(1162368000000, 3950),
array(1164960000000, 4050), array(1167638400000, 4150),
array(1170316800000, 4250),
array(1172736000000, 4350), array(1175414400000, 4450),
array(1178002800000, 4550),
array(1180681200000, 4650), array(1183273200000, 4750),
array(1185951600000, 4850),
array(1188630000000, 4950), array(1191222000000, 5050),
array(1193904000000, 5150),
array(1196496000000, 5250), array(1199174400000, 5350),
array(1201852800000, 5450),
array(1204358400000, 5550), array(1207036800000, 5650),
array(1209625200000, 5750),
array(1212303600000, 5850), array(1214895600000, 5950),
array(1217574000000, 6050),
array(1220252400000, 6150));
$now = time() . substr(microtime(), 2, 3);
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
?>
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
?>
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
?>
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
?>
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 > 1 ? 's' : '') . ' ago';
}elseif($today){
$num = floor($diff / 60 / 60);
return $num . ' hour' . ($num > 1 ? 's' : '') . ' ago';
}else{
$thisyear = date('y', $now) === date('y', $time) ? true : false;
$daydiff = date('z', $now) - date('z', $time);
if($daydiff === 1 && $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
?>
1 ... « 2 3 4 5 6 » ... 10