PHP age calculator / Alter berechnen

<?php
/**
 * age_calculator()
 *
 * @param int $day DD
 * @param int $month MM
 * @param int $year YYYY
 * @return int $age years
 */
function age_calculator($day,$month,$year){
	if(date("n") > $month){
		$age = (date("Y") - $year);
	}
	elseif(date("n") == $month){
 		if(date("j") < $day){
			$age = (date("Y") - $year) - 1;
		}
 		else
		{
		 	$age = (date("Y") - $year);
		}
 	}
	else
	{
		$age = (date("Y") - $year) - 1;
	}

	return $age;
}
?>

PHP crypt command line

crypt

#!/usr/bin/php
< ?php
require "crypt.php";

$type = $argv['1'];
$string = $argv['2'];
$key = $argv['3'];
if($type !='' &&  $string != '' && $key != ''){
        if($type=="e"){
                echo encrypt($string,$key);
                echo "\n";
        }
        elseif($type=="d")
        {
                echo decrypt($string,$key);
                echo "\n";
        }
        else
        {
                die('WRONG TYPE');
        }
}
else
{
        echo 'crypt TYPE STRING KEY';
        echo "\n\n";
        echo "TYPE:\n";
        echo "e encrypt\n";
        echo "d decrypt\n";
        echo "\n\n";
        echo "STRING Your string\n";
        echo "KEY Crypt key\n\n";
}
?>

crypt.php

< ?php
/**
 * encrypt()
 *
 * @param mixed $string
 * @param mixed $key
 * @return mixed $retrun
 */
function encrypt($string, $key){

        $result = '';
        $lentgh = strlen($string);
        for($i = 0; $i < $lentgh; $i++) {
                $char = substr($string, $i, 1);
                $keychar = substr($key, ($i % strlen($key))-1, 1);
                $char = chr(ord($char) + ord($keychar));
                $result .= $char;
        }

        return base64_encode($result);
}

/**
 * decrypt()
 *
 * @param mixed $string
 * @param mixed $key
 * @return mixed $return
 */
function decrypt($string, $key){

        $result = '';
        $string = base64_decode($string);
        $lentgh = strlen($string);

        for($i = 0; $i < $lentgh; $i++) {
                $char = substr($string, $i, 1);
                $keychar = substr($key, ($i % strlen($key))-1, 1);
                $char = chr(ord($char) - ord($keychar));
                $result .= $char;
        }

        return $result;
}
?>

PHP cache class

< ?php
/**
*
* @var $cache_file_name string file name
* @var $age int age in seconds
*/
class cache {

	public $cache_file_name;
	public $age;

	/**
	* cache::__construct()
	* @access public
	*/
	public function __construct(){
		$this->cache_start();
		register_shutdown_function(array($this, "cache_end"), "inside");
	}

	/**
	* cache::__descruct()
	* @access public
	* @return
	*/
	public function __descruct(){
		$this->cache_end();
	}

	/**
	* cache::cache_start()
	* @access public
	* @return
	*/
	public function cache_start(){

		global $cache_file_name, $age;
		$cache_file_name = 	$_SERVER["DOCUMENT_ROOT"].$_SERVER['REQUEST_URI'] . '_cache';

		if (empty($age)){
			$age = 600;
		}

		if(file_exists($cache_file_name)){
			if (filemtime($cache_file_name) + $age > time()) {
				readfile($cache_file_name);
				unset($cache_file_name);
				exit;
			}
		}
		ob_start();
	}

	/**
	* cache::cache_end()
	* @access public
	* @return
	*/
	public function cache_end()
	{
		global $cache_file_name;
		if (empty($cache_file_name)){
			return;
		}
		$str = ob_get_clean();
		echo $str;
		fwrite(fopen($cache_file_name . '_tmp', "w"), $str);
		rename($cache_file_name . '_tmp',$cache_file_name);
	}

}

?>

Doppelte Werte in einem array löschen

function remove_dup($matriz) {
   $aux_ini=array();
   $entrega=array();
   for($n=0;$n<count($matriz);$n++) {
      $aux_ini[]=serialize($matriz[$n]);
   }
   $mat=array_unique($aux_ini);
   for($n=0;$n<count($matriz);$n++) {
      $entrega[]=unserialize($mat[$n]);
   }
   foreach ($entrega as $key => $row){
      if (!is_array($row)) { unset($entrega[$key]); }
   }
   return $entrega;
}

Archive for category php

Archives by Month: