BigToach.com

PHP Snippets

1 2 3 » ... 5

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 - 4 Comments
Updated on Nov 4th at 4 am.


image_borders

Information

(PHP Version >= 4.1.6)
Functions used in this snippet: is_array, strtolower, substr, strrpos, imagesx, imagesy, imagecreate, imagecopy, min.

Description

int image_borders( int img, array borders )

Adds borders to the image img. The borders array should have the indexes tl, tt, tr, rr, br, bb, bl, ll correspoding to that area's border. (see the example below for a better explanation)

Note: This function is fairly slow, so it would be best to apply it once, then save it for future showings.

Also, the wider the top(tt) and bottom(bb) and the longer the left(ll) and right(rr) borders are the faster the function will perform. They CAN be longer and wider than the actual image itself.

Snippet

<?php
function image_borders($img$borders){
    if(!
is_array($borders)){
        return 
$img;
    }
    
$border_data = array();
    
$types = array('jpeg'=>'imagecreatefromjpeg''jpg'=>'imagecreatefromjpeg''gif'=>'imagecreatefromgif''png'=>'imagecreatefrompng');
    foreach(
$borders as $key=>$value){
        
$border_data[$key]['img'] = $types[strtolower(substr($valuestrrpos($value'.') + 1))]($value);
        
$border_data[$key]['x']   = imagesx($border_data[$key]['img']);
        
$border_data[$key]['y']   = imagesy($border_data[$key]['img']);
    }
    
$x imagesx($img);
    
$y imagesy($img);
    
$dest imagecreate(($x $border_data['ll']['x'] + $border_data['rr']['x']), ($y $border_data['tt']['y'] + $border_data['bb']['y']));
    
$max_x $x $border_data['ll']['x'];
    
$actual_x $border_data['ll']['x'];
    
$ttx $border_data['ll']['x'];
    
$bbx $border_data['ll']['x'];
    while(
$actual_x $max_x){
        if(
$ttx $max_x){
            
imagecopy($dest$border_data['tt']['img'], $ttx000$border_data['tt']['x'], $border_data['tt']['y']);
            
$ttx += $border_data['tt']['x'];
        }
        if(
$bbx $max_x){
            
imagecopy($dest$border_data['bb']['img'], $bbx, ($border_data['ll']['y'] + $y), 00$border_data['bb']['x'], $border_data['bb']['y']);
            
$bbx += $border_data['bb']['x'];
        }
        
$actual_x min($ttx$bbx);
    }
    
$max_y $y $border_data['tt']['y'];
    
$actual_y $border_data['tt']['y'];
    
$lly $border_data['tt']['y'];
    
$rry $border_data['rr']['y'];
    while(
$actual_y $max_y){
        if(
$lly $max_y){
            
imagecopy($dest$border_data['ll']['img'], 0$lly00$border_data['ll']['x'], $border_data['ll']['y']);
            
$lly += $border_data['ll']['y'];
        }
        if(
$rry $max_y){
            
imagecopy($dest$border_data['rr']['img'], ($border_data['ll']['x'] + $x), $rry0,0$border_data['rr']['x'], $border_data['rr']['y']);
            
$rry += $border_data['rr']['y'];
        }
        
$actual_y min($lly$rry);
    }
    
imagecopy($dest$border_data['tl']['img'], 0000$border_data['tl']['x'], $border_data['tl']['y']);
    
imagecopy($dest$border_data['tr']['img'], ($x $border_data['tl']['x']), 000$border_data['tr']['x'], $border_data['tr']['y']);
    
imagecopy($dest$border_data['bl']['img'], 0, ($y $border_data['tl']['y']), 00$border_data['bl']['x'], $border_data['bl']['y']);
    
imagecopy($dest$border_data['br']['img'], ($x $border_data['tl']['x']), ($y $border_data['tl']['y']), 00$border_data['br']['x'], $border_data['br']['y']);
    
imagecopy($dest$img$border_data['ll']['x'], $border_data['tt']['y'], 00$x$y);
    return 
$dest;
}
?>

Example

image_borders Can be used in the following way:

<?php
$img 
imagecreatefromjpeg('baseimage.jpg');
// create the base image

$borders = array(    'tl'=>'topleft.jpg',
                    
'tt'=>'top.jpg',
                    
'tr'=>'topright.jpg',
                    
'rr'=>'right.jpg',
                    
'br'=>'bottomright.jpg',
                    
'bb'=>'bottom.jpg',
                    
'bl'=>'bottomleft.jpg',
                    
'll'=>'left.jpg');
// create your array with borders
// Note, the array indexes must be the same as above

$dest image_borders($img$borders);
// actually make the image

header('Content-type: image/png');
// send a header

imagepng($dest);
// send it to the browser
?>

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


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


dir_recurse

Information

(PHP Version >= 3)
Functions used in this snippet: opendir, readdir, is_dir, dir_recurse, filesize, closedir, isset.

Description

array dir_recurse( str dir, bool filesize )

Recursively find all files and folders underneath the folder dir.
If the optional filesize is set to true the array will have the indexes be the filenames, and the values be the filsize of the filenames.

Snippet

<?php
function dir_recurse($dir$filesize=false){
    if (
$fh opendir($dir)) {
        while(
false !== ($file readdir($fh))){
            if(
$file != "." && $file != "..") {
                if(
is_dir($dir '/' $file)){
                    
$data[$file] = dir_recurse($dir '/' $file$filesize);
                }else{
                    if(
$filesize){
                        
$data[$file] = filesize($dir '/' $file);
                    }else{
                        
$data[] = $file;
                    }
                }
            }
        }
        
closedir($fh);
    }
    return (isset(
$data) ? $data : array());
}
?>

Example

dir_recurse Can be used in the following way:

<?php
print_r
(dir_recurse('.')); 
// returns the directory structure of the directory that the script is in

print_r(dir_recurse('some/path/to/dir'));
// returns dir structure of some/path/to/dir

print_r(dir_recurse('.'true)); 
// returns filesizes of all files above the current working directory.

// if you have PHP 5 and you want to easily show the dir folder size you can do the following

function dummy_func($value){
    global 
$_dummy_var;
    
$_dummy_var += $value;
}
array_walk_recursive(dir_recurse('.'), 'dummy_func');
echo 
'That directory is ' $_dummy_var ' Bytes big';
?>

Added on Jul 10th at 9 pm by Scott - 5 Comments


random_sig

Information

(PHP Version >= 4.3.0)
Functions used in this snippet: array_rand, getimagesize, header, image_type_to_mime_type, readfile, is_numeric.

Description

mixed random_sig( bool image )

If the optional image arguement is left as true, random_sig will output a random image. If image is set to false it will output the html tags for an image, and link if a link is supplied.

(note you cannot have more than one image pointing to the same link)

Snippet

<?php
function random_sig($image=true){
    
$array = array(    'http://www.google.com' => 'google.gif',
                    
'someimage.jpg',
                    
'anotherimage.png');
    
$rand array_rand($array);
    
$size getimagesize($array[$rand]);
    if(
$image){
        
header('Content-type: ' image_type_to_mime_type($size[2]));
        
readfile($array[$rand]);
        exit;
    }else{
        if(!
is_numeric($rand)){
            return 
'<a href="' $rand '"><img src="' $array[$rand] . '" alt="" /></a>';
        }else{
            return 
'<img src="' $array[$rand] . '" alt="" />';
        }
    }
    return 
false;
}
?>

Example

random_sig Can be used in the following way:

<?php
echo random_sig(false); // outputs <img src="" /> etc

random_sig(); // outputs an actual image.
// the above can be used like <img src="your_file_name.php" />
?>

Added on Jul 10th at 8 pm by Scott - 0 Comments


1 2 3 » ... 5