» Birthday Bundle - Over $400 worth of Envato files for just $20
  Posts tagged 'calculate'

Hello coders,

This is a PHP Class that calculates the size, number of files & folders of a specific directory.

calculate.directory.class.php

<?php
/*
Credits: BitRepository.com
URL: http://www.bitrepository.com/web-programming/php/calculate-the-size-number-of-files-folders-of-a-directory.html
*/

class Directory_Calculator {

    var $size_in;
	var $decimals;

    function calculate_whole_directory($directory)
    {
        if ($handle = opendir($directory))
        {
        $size = 0;
		$folders = 0;
		$files = 0;

        while (false !== ($file = readdir($handle)))
		{
            if ($file != "." && $file != "..")
			{
			    if(is_dir($directory.$file))
			    {
                $array = $this->calculate_whole_directory($directory.$file.'/');
                $size += $array['size'];
				$files += $array['files'];
				$folders += $array['folders'];
			    }
			    else
			    {
                $size += filesize($directory.$file);
				$files++;
			    }
            }
         }
         closedir($handle);
         }

		 $folders++;

    return array('size' => $size, 'files' => $files, 'folders' => $folders);
    }

	function size_calculator($size_in_bytes)
    {
        if($this->size_in == 'B')
        {
        $size = $size_in_bytes;
        }
        elseif($this->size_in == 'KB')
        {
        $size = (($size_in_bytes / 1024));
        }
        elseif($this->size_in == 'MB')
        {
        $size = (($size_in_bytes / 1024) / 1024);
        }
        elseif($this->size_in == 'GB')
        {
        $size = (($size_in_bytes / 1024) / 1024) / 1024;
        }

        $size = round($size, $this->decimals);

		return $size;
	}

	function size($directory)
	{
	$array = $this->calculate_whole_directory($directory);
	$bytes = $array['size'];
	$size = $this->size_calculator($bytes);
	$files = $array['files'];
	$folders = $array['folders'] - 1; // exclude the main folder

	return array('size'    => $size,
		         'files'   => $files,
		         'folders' => $folders);
	}
}
?>

Read more from this entry…

Calculate percent from value

Posted on August 31, 2008, Filled under PHP,  Bookmark it

Here’s how you can calculate the percent from a value.

<?php
/* Credits: http://www.bitrepository.com */

// example: number of employees in a company's department (Marketing)
$value = '20';
// total employees that company has
$total = '500'; 

$percent = number_format(($value * 100) / $total);

// percent of employees from the Marketing Department.
echo $percent.'%';
?>

Calculating value from percent

Posted on August 31, 2008, Filled under PHP,  Bookmark it

This short tutorial is aimed to help you how to calculate the percent value from a total. Let’s suppose you have a e-commerce site and you offer discounts to the customers. You need to show them how much is the discount value worthing.

Here’s a sample from an order total:

Notebook’s price: $1,300
Our special discount: 30% (-$390)
Final price: $910

Here’s the script which calculates the special discount value and the final price:

<?php
$percent = '30'; // without %
$total = '1300'; // initial value

/* Calculate $percent% from $total */
$discount_value = ($total / 100) * $percent;

$final_price = $total - $discount_value;

// Format numbers with number_format()

$total = number_format($total);
$discount_value = number_format($discount_value);
$final_price = number_format($final_price);

echo "Notebook's price: $".$total."<br />Our special discount: <strong>".
$percent."%</strong> (-$".$discount_value.")<br />Final price: $".$final_price;
?>

Any comments and suggestions regarding this script are welcomed.

How to calculate age in PHP

Posted on August 29, 2008, Filled under PHP,  Bookmark it

Here’s a simple php function that calculates the age of someone, in years.

<?php
/*
Credits: http://www.bitrepository.com
URL: http://www.bitrepository.com/web-programming/php/simple-age-calculator.html
*/
function determine_age($birth_date)
{
$birth_date_time = strtotime($birth_date);
$to_date = date('m/d/Y', $birth_date_time);

list($birth_month, $birth_day, $birth_year) = explode('/', $to_date);

$now = time();

$current_year = date("Y");

$this_year_birth_date = $birth_month.'/'.$birth_day.'/'.$current_year;
$this_year_birth_date_timestamp = strtotime($this_year_birth_date);

$years_old = $current_year - $birth_year;

if($now < $this_year_birth_date_timestamp)
{
/* his/her birthday hasn't yet arrived this year */
$years_old = $years_old - 1;
}

return $years_old;
}

// You can write about any English textual datetime description

$birth_date = '1 May 1980';

$age = determine_age($birth_date);

echo $age;
?>