Restzeiten mit PHP berechnen (OOP)
Da mir zur Zeit extrem langweilig ist, habe ich mich an eine Klasse zur Berechnung von Zeitunteschieden (Restzeit ;)) gemacht. Sie ist nicht die beste, hat aber die wichtigsten Funktionen eingebaut. Die Parameter sollten selbsterklärend sein….
resttime.class.php
define("SECONDS_PER_MINUTE", 60);
define("SECONDS_PER_MINUTE", 60);
define("SECONDS_PER_HOUR", SECONDS_PER_MINUTE*60);
define("SECONDS_PER_DAY", SECONDS_PER_HOUR*24);
define("SECONDS_PER_MONTH", SECONDS_PER_DAY*30);
define("SECONDS_PER_WEEK", SECONDS_PER_DAY*7);
define("SECONDS_PER_YEAR", SECONDS_PER_MONTH*12);
/**
Klasse zur Berechnung von Restzeiten
*/
class resttime
{
private $start_timestamp; //start of the period
private $end_timestamp; //end of the period
private $difference; //difference between start and end
private $rest; //temporary variable for calculations
private $days; //amount of days
private $hours; //amount of hours
private $minutes; //amount of minuts
private $seconds; //amount of seconds
private $years; //amount of years
private $months; //amount of months
private $weeks; //amount of weeks
/**
Constructor initializes properities
*/
public function __construct($start = 0, $end = 0) //Start and end can be defined in the constructor (it's no must)
{
$this->start_timestamp = 0;
$this->end_timestamp = 0;
$this->difference = 0;
$this->rest = 0;
$this->days = 0;
$this->hours = 0;
$this->minutes = 0;
$this->seconds = 0;
$this->years = 0;
$this->months = 0;
$this->weeks = 0;
/**
That's the default value, wich can be changed by calling $class->set_timezone()
*/
$this->set_timezone("Europe/Berlin");
/**
To ensure that a start is defined in any case, set_start is called
*/
$this->set_start($start);
/**
To ensure that a end is defined in any case, set_end is called
*/
$this->set_end($end);
}
/**
Destructor deletes the variables
*/
public function __destruct()
{
unset($this->start_timestamp);
unset($this->end_timestamp);
unset($this->difference);
unset($this->rest);
unset($this->days);
unset($this->hours);
unset($this->minutes);
unset($this->seconds);
unset($this->years);
unset($this->months);
unset($this->weeks);
}
/**
This function is only used when this class has to work with a date, wich is no timestamp
*/
public function set_timezone($zone)
{
date_default_timezone_set($zone);
}
/**
Sets the beginning of the time to calculate
*/
public function set_start($timestamp)
{
if(is_numeric($timestamp)) //no datetime, so there's no need to convert the date to a timestamp
{
$this->start_timestamp = $timestamp;
}
else
{
$this->start_timestamp = strtotime($timestamp); //convert the date to a timestamp
}
$this->calculate_difference(); //update the difference in this object
}
/**
Returns a timestamp
*/
public function get_start()
{
return $this->start_timestamp;
}
/**
Sets the end of the time to calculate
*/
public function set_end($timestamp)
{
if(is_numeric($timestamp)) //no datetime, so there's no need to convert the date to an timestamp
{
$this->end_timestamp = $timestamp;
}
else
{
$this->end_timestamp = strtotime($timestamp); //convert the date to a timestamp
}
$this->calculate_difference(); //update the difference in this object
}
/**
Returns a timestamp
*/
public function get_end()
{
return $this->end_timestamp;
}
/**
This private function calculates the difference between the end and the start of the period
*/
private function calculate_difference()
{
$this->difference = $this->end_timestamp - $this->start_timestamp;
}
/**
Returns the calculated difference as integer (in seconds)
*/
public function get_difference()
{
return (int)$this->difference;
}
/**
Returns the calculated difference as an array / a string
*/
public function get_difference_formated($array = true,
$year = array(true, true), //1. parameter means: Yes, i want to have it
$month = array(true, true), //2. parameter means: Yes, i want to have it even when it's zero
$week = array(true, true),
$day = array(true, true),
$hour = array(true, true),
$minute = array(true, true),
$second = array(true, true))
{
$this->rest = $this->difference;
if($year[0] == true)
{
$this->years = floor($this->rest / SECONDS_PER_YEAR);
$this->rest = $this->rest - ($this->years * SECONDS_PER_YEAR);
}
if($month[0] == true)
{
$this->months = floor($this->rest / SECONDS_PER_MONTH);
$this->rest = $this->rest - ($this->months * SECONDS_PER_MONTH);
}
if($week[0] == true)
{
$this->weeks = floor($this->rest / SECONDS_PER_WEEK);
$this->rest = $this->rest - ($this->weeks * SECONDS_PER_WEEK);
}
if($day[0] == true)
{
$this->days = floor($this->rest / SECONDS_PER_DAY);
$this->rest = $this->rest - ($this->days * SECONDS_PER_DAY);
}
if($hour[0] == true)
{
$this->hours = floor($this->rest / SECONDS_PER_HOUR);
$this->rest = $this->rest - ($this->hours * SECONDS_PER_HOUR);
}
if($minute[0] == true)
{
$this->minutes = floor($this->rest / SECONDS_PER_MINUTE);
$this->rest = $this->rest - ($this->minutes * SECONDS_PER_MINUTE);
}
if($second[0] == true)
{
$this->seconds = $this->rest;
}
if($array != true)
{ //when it's not 0 OR when you want to have it even when it's 0
return (($this->years != 0 || $year[1]) ? $this->years." Years, " : "").
(($this->months != 0 || $month[1]) ? $this->months." Months " : "").
(($this->weeks != 0 || $week[1]) ? $this->weeks." Weeks " : "").
(($this->days != 0 || $day[1]) ? $this->days." Days " : "").
(($this->hours != 0 || $hour[1]) ? $this->hours." Hours " : "").
(($this->minutes != 0 || $minute[1]) ? $this->minutes." Minutes " : "").
(($this->seconds != 0 || $second[1]) ? $this->seconds." Seconds " : "0 Seconds"); //falls Unterschied 0 ist, soll kein leerer string returned werden
}
else
{
return array("years" => $this->years,
"months" => $this->months,
"weeks" => $this->weeks,
"days" => $this->days,
"hours" => $this->hours,
"minutes" => $this->minutes,
"seconds" => $this->seconds);
}
}
}
Und hier ein Beispiel zur Verwendung der Klasse:
index.php
error_reporting(E_ALL | E_STRICT);
error_reporting(E_ALL | E_STRICT);
require_once("resttime.class.php");
for($i = 0; $i < 30; $i++)
{
$test[$i] = new Resttime;
}
for($i = 0; $i < 30; $i++)
{
$test[$i]->set_start(date("d.m.Y H:i:s", time()-mt_rand(1, 9999999)*20));
$test[$i]->set_end("04.02.2009 23:00:00");
echo "
".$test[$i]->get_difference_formated(false, array(true, true),
array(true, true),
array(true, true),
array(true, true),
array(true, true),
array(true, true),
array(true, true));
}
[…] Restzeiten mit PHP berechnen (OOP) (34) […]
[…] Restzeiten mit PHP berechnen (OOP) (73) […]