Mal wieder was neues, das ich heute aus langeweile geschrieben habe. Das ganze ist ein Objekt, das die Spielzeit anhand der wirklichen Zeit berechnet. Bis jetzt kann man nur das Datum und die Zeit bekommen, aber ich plane, auch noch die wichtigen restlichen Möglichkeiten von date() zu implementieren. Das einzige, was ich vermutlich nie implementieren werde, sind Schaltjahre.
Die Einstellmöglichkeiten sind vielfältig:
- Dauer einer Spielsekunde je Realer Sekunde
- Anzahl Sekunden je Minute
- Anzahl Minuten je Stunde
- Anzahl Tage pro Woche
- Tage pro Monate (Als Array, die Anzahl Elemente sind die Anzahl Monate, die einzelnen Werte die Anzahl Tage)
- Anzahl Monate pro Jahr
- Startjahr des Zeitstempfels (Wann war 1970? Für ein Spieljahr von 1809 muss als Startjahr 1770 angegeben werden)
$this->bbcode_second_pass_code('', '<?php
/* Gametime-Object
* Calculates the ingame date and time based on the real timestamp
*
* Version 1.0, 30. Oktober 2009
* 2009 by Basilius "Wasili" Sauter
* Licensed unter the terms of the GNU GPL 2
*/
class Gametime {
public static $secondsPerMinute = 60;
public static $minutesPerHour = 60;
public static $hoursPerDay = 24;
public static $daysPerWeek = 7;
public static $daysPerMonth = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
public static $monthsPerYear = 12;
public static $firstYear = 1970;
public static $secondsPerRealSecond = 1;
public static $daysPerYear = NULL;
public static $secondsPerDay = NULL;
public static $secondsPerYear = NULL;
protected $timestamp = 0;
public function __construct($timestamp = false) {
self::loadGametimeSettings();
if($timestamp === false) {
$this->timestamp = microtime(true)/self::$secondsPerRealSecond;
}
}
protected static function loadGametimeSettings() {
// Get Basic Settings
self::$secondsPerMinute = GAMETIME_SPM;
self::$minutesPerHour = GAMETIME_MPH;
self::$hoursPerDay = GAMETIME_HPD;
self::$daysPerWeek = GAMETIME_DPW;
self::$daysPerMonth = unserialize(GAMETIME_DPM);
self::$monthsPerYear = GAMETIME_MPY;
self::$firstYear = GAMETIME_FY;
self::$secondsPerRealSecond = GAMETIME_SPR;
// Calculate other values
self::$daysPerYear = array_sum(self::$daysPerMonth);
self::$secondsPerDay = self::$secondsPerMinute * self::$minutesPerHour * self::$hoursPerDay;
self::$secondsPerYear = self::$secondsPerDay * self::$daysPerYear;
}
public function getDate($a) {
// Replace Year
$a = str_replace('Y', $this->getYear(), $a);
$a = str_replace('y', substr($this->getYear(), -2), $a);
// Replace month
$a = str_replace('m', sprintf('%02d', $this->getMonth()), $a);
// Replace day
$a = str_replace('d', sprintf('%02d', $this->getDay()), $a);
// Replace hour
$a = str_replace('H', sprintf('%02d', $this->getHour()), $a);
// Replace minute
$a = str_replace('i', sprintf('%02d', $this->getMinute()), $a);
// Replace second
$a = str_replace('s', sprintf('%02d', $this->getSecond()), $a);
return $a;
}
protected function getYear() {
return self::$firstYear + floor($this->timestamp/self::$secondsPerYear);
}
protected function getMonth() {
// Get the day of the Year.
$d = $this->getDayOfYear();
// Get the month!
$m = 0;
foreach(self::$daysPerMonth as $days) {
$m++;
if($d - $days > 0) {
$d-=$days;
continue;
}
else {
break;
}
}
return $m;
}
protected function getDay() {
// Get the day of the Year.
$d = $this->getDayOfYear();
// Get the month!
$m = 0;
foreach(self::$daysPerMonth as $days) {
$m++;
if($d - $days > 0) {
$d-=$days;
continue;
}
else {
break;
}
}
return $d;
}
protected function getHour() {
$secondsToday = $this->getSecondsToday();
return floor($secondsToday / (self::$minutesPerHour * self::$secondsPerMinute));
}
protected function getMinute() {
$secondsToday = $this->getSecondsToday();
$secondsInHour = $secondsToday - $this->getHour()*(self::$minutesPerHour * self::$secondsPerMinute);
return floor($secondsInHour / (self::$minutesPerHour));
}
protected function getSecond() {
$secondsToday = $this->getSecondsToday();
$secondsLeft = $secondsToday - $this->getHour()*(self::$minutesPerHour * self::$secondsPerMinute);
$secondsLeft = $secondsLeft - $this->getMinute()*self::$secondsPerMinute;
return round($secondsLeft, 0);
}
protected function getDayOfYear() {
return floor(($this->timestamp - self::$secondsPerYear * floor($this->timestamp/self::$secondsPerYear)) / self::$secondsPerDay);
}
protected function getSecondsToday() {
return $this->timestamp - self::$secondsPerDay * floor($this->timestamp/self::$secondsPerDay);
}
}
')
Beispiel:
$this->bbcode_second_pass_code('', '
header('Content-type: text/plain');
// Menschliche Zeitrechnung
define('GAMETIME_SPM', 60);
define('GAMETIME_MPH', 60);
define('GAMETIME_HPD', 24);
define('GAMETIME_DPW', 7);
define('GAMETIME_DPM', serialize(array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)));
define('GAMETIME_MPY', 12);
define('GAMETIME_FY', 1970);
define('GAMETIME_SPR', 1);
$gt = new Gametime();
printf("Die aktuelle Zeit in menschlicher Zeitrechnung ist %s", $gt->getDate('Y-m-d H:i:s'));
')
Gibt für heute (30. Oktober 2009) aus (Schalttage fehlen!):
$this->bbcode_second_pass_code('', 'Die aktuelle Zeit in menschlicher Zeitrechnung ist 2009-11-08 00:17:33')