Tuesday, July 13, 2010

Ago Timestamp PHP function

A php function to find how much time ago the timestamp was

function ago($timestamp){
$difference = time() - $timestamp;
$periods = array("second", "minute", "hour", "day", "week", "month", "years", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
for($j = 0; $difference >= $lengths[$j]; $j++)
$difference /= $lengths[$j];
$difference = round($difference);
if($difference != 1) $periods[$j].= "s";
$text = "$difference $periods[$j] ago";
return $text;
}

iPad Browser simulator

Hi

Please see this useful website to simulate any website on ipad

http://ipadpeek.com/

Get User Browser PHP function

PHP function to get user browser


function get_user_browser()
{
$u_agent = $_SERVER['HTTP_USER_AGENT'];
$ub = '';
if(preg_match('/MSIE/i',$u_agent))
{
$ub = "ie";
}
elseif(preg_match('/Firefox/i',$u_agent))
{
$ub = "firefox";
}
elseif(preg_match('/Safari/i',$u_agent))
{
$ub = "safari";
}
elseif(preg_match('/Chrome/i',$u_agent))
{
$ub = "chrome";
}
elseif(preg_match('/Flock/i',$u_agent))
{
$ub = "flock";
}
elseif(preg_match('/Opera/i',$u_agent))
{
$ub = "opera";
}

return $ub;
}

Resize Image Function

Function to resize an image

function resize_imageGD($ext,$from,$from_w,$from_h,$to,$to_w,$to_h){

// create an image of the given filetype
if ($ext == 'jpg' || $ext == 'jpeg'){
if(!function_exists("imagecreatefromjpeg")) return 0;
$image = @imagecreatefromjpeg($from);
}elseif($ext == 'png') {
if(!function_exists("imagecreatefrompng")) return 0;
$image = @imagecreatefrompng($from);

}elseif($ext == 'gif') {
if(!function_exists("imagecreatefromgif")) return 0;

$image = @imagecreatefromgif($from);
}
if(!$image) return 0;

if(function_exists("imagecreatetruecolor")){
$newimg = @imagecreatetruecolor($to_w, $to_h);
}
if(!$newimg) $newimg = @imagecreate($to_w, $to_h);
if(!$newimg){
imagedestroy($image);
return 0;
}

//keep png alpha channel if possible
if($ext == 'png' && function_exists('imagesavealpha')){
imagealphablending($newimg, 0);
imagesavealpha($newimg,true);
}

//try resampling first
if(function_exists("imagecopyresampled")){
if(!@imagecopyresampled($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h)) {
imagecopyresized($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h);
}
}else{
imagecopyresized($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h);
}

$okay = 0;
if ($ext == 'jpg' || $ext == 'jpeg'){
if(!function_exists('imagejpeg')){
$okay = 0;
}else{
$okay = imagejpeg($newimg, $to);
}
}elseif($ext == 'png') {
if(!function_exists('imagepng')){
$okay = 0;
}else{
$okay = imagepng($newimg, $to);
}
}elseif($ext == 'gif') {
if(!function_exists('imagegif')){
$okay = 0;
}else{
$okay = imagegif($newimg, $to);
}
}

// destroy GD image ressources
if($image) imagedestroy($image);
if($newimg) imagedestroy($newimg);

return $okay;
}

Thursday, January 18, 2007

PHP Function to format currency

Here is a function to format

function format_currency($curr){
$formated_value = number_format($curr, 2, '.', ',');
return $formated_value;
}

PHP function to get extension of a file

Here is a simple usefull function to get extension of a file

function getFileExtension($filename){
$dot = substr (strrchr ($filename, "."), 1);
return ($dot);
}

PHP function see if url exists

Here is an simple usefull function see if an url exists

function url_exists($url)
{
$handle = @fopen($url, "r");
if ($handle === false)
return false;
fclose($handle);
return true;
}