From 356ca3b2ffea921588756521e62ee2f6397c4ce2 Mon Sep 17 00:00:00 2001 From: Gerald Simane Date: Fri, 29 Jan 2010 13:47:44 +0000 Subject: [PATCH] --- include/datum.class.php | 503 ++++++++++++++++++++++------------------ 1 file changed, 279 insertions(+), 224 deletions(-) diff --git a/include/datum.class.php b/include/datum.class.php index 50017a0be..0cfb82622 100644 --- a/include/datum.class.php +++ b/include/datum.class.php @@ -1,225 +1,280 @@ -, - * Andreas Oesterreicher and - * Rudolf Hangl . - */ - -class datum -{ - public $ts_day=86400; // Timestamp eines Tages - - /** - * Konstruktor - * - */ - public function __construct() - { - } - - /** - * Liefert einen UNIX Timestamp von einem String im - * Format "31.12.2007 14:30" - */ - public function mktime_datumundzeit($datumundzeit) - { - if(mb_ereg("([0-9]{2}).([0-9]{2}).([0-9]{4}) ([0-9]{2}):([0-9]{2})",$datumundzeit, $regs)) - return mktime($regs[4],$regs[5],0,$regs[2],$regs[1],$regs[3]); - else - { - $this->errormsg = 'Falsches Datumsformat'; - return false; - } - } - - /** - * Liefert einen UNIX Timestamp von einem String im - * Format "31.12.2007" - */ - public function mktime_datum($datum) - { - if(mb_ereg("([0-9]{2}).([0-9]{2}).([0-9]{4})",$datum, $regs)) - { - return mktime(0,0,0,$regs[2],$regs[1],$regs[3]); - } - else - { - $this->errormsg = 'Falsches Datumsformat'; - return false; - } - } - - /** - * Liefert einen UNIX Timestamp von einem Datum im - * ISO-Format "2007-01-31" - */ - public function mktime_fromdate($datum) - { - if(mb_ereg("([0-9]{4})-([0-9]{2})-([0-9]{2})",$datum, $regs)) - { - return mktime(0,0,0,$regs[2],$regs[3],$regs[1]); - } - else - { - $this->errormsg = 'Falsches Datumsformat'; - return false; - } - } - - /** - * Liefert einen UNIX Timestamp von einem String im - * Format "2007-01-31 14:30:12" - */ - public function mktime_fromtimestamp($timestamp) - { - if(mb_ereg("([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})",$timestamp, $regs)) - { - return mktime($regs[4],$regs[5],$regs[6],$regs[2],$regs[3],$regs[1]); - } - else - { - $this->errormsg = 'Falsches Datumsformat'; - return false; - } - } - - /** - * Springt von einen UNIX Timestamp ($datum) $wochen nach vor bzw. hinten - */ - public function jump_week($datum, $wochen) - { - $stunde_vor=date("G",$datum); - // Eine Woche sind 604800 Sekunden - $datum+=604800*$wochen; - $stunde_nach=date("G",$datum); - if ($stunde_nach!=$stunde_vor) - $datum+=3600; - return $datum; - } - - /** - * Springt von einen UNIX Timestamp ($datum) $days nach vor bzw. hinten - */ - public function jump_day($datum, $days) - { - $stunde_vor=date("G",$datum); - // Ein Tag sind 86400 Sekunden - $datum+=86400*$days; - $stunde_nach=date("G",$datum); - if ($stunde_nach!=$stunde_vor) - $datum+=3600; - return $datum; - } - - /** - * Konvertiert das ISO Datumsformat (YYYY-MM-DD) - * nach (DD.MM.YYYY) - */ - public function convertISODate($datum) - { - return (mb_strlen($datum)>0?date('d.m.Y',strtotime($datum)):''); - } - - - /** - * Prueft Uhrzeit auf Gueltigkeit (HH:MM:SS) - * @return true wenn ok, false wenn falsches Format - */ - public function checkUhrzeit($uhrzeit) - { - if(mb_ereg("([0-9]{2}):([0-9]{2})(:([0-9]{2}))?$",$uhrzeit)) - return true; - else - return false; - } - - /** - * Prueft ob das Datum im Format dd.mm.YYYY oder YYYY-mm-dd ist - * @return true wenn ok, false wenn falsches Format - */ - public function checkDatum($datum) - { - if(mb_ereg("([0-9]{4})-([0-9]{2})-([0-9]{2})$",$datum) || mb_ereg("([0-9]{2}).([0-9]{2}).([0-9]{4})$",$datum)) - return true; - else - return false; - } - - /** - * Liefert ein Datum im angegeben Format - * ToDo: Liefert aktuellen Timestamp wenn Sonderzeichen uebergeben werden - * zB '---' - * @param $datum - * @param $format - * @param $strict wenn das Datum aus einem Suchfeld komment, dann strict auf TRUE setzen da sonst - * Eintraege wie zB 'last Monday' oder 'a' auch in ein Datum umgewandelt werden. - * @return Formatierten Timestamp wenn ok, false im Fehlerfall - */ - public function formatDatum($datum, $format='Y-m-d H:i:s', $strict=false) - { - if(trim($datum)=='') - return ''; - - $ts=''; - $error=false; - - //2008-12-31 - if(mb_ereg("([0-9]{4})-([0-9]{2})-([0-9]{2})",$datum, $regs)) - $ts = mktime(0,0,0,$regs[2],$regs[3],$regs[1]); - - //2008-12-31 12:30 - if(mb_ereg("([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2})",$datum, $regs)) - $ts = mktime($regs[4],$regs[5],0,$regs[2],$regs[3],$regs[1]); - - //2008-12-31 12:30:15 - if(ereg("([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})",$datum, $regs)) - $ts = mktime($regs[4],$regs[5],$regs[6],$regs[2],$regs[3],$regs[1]); - - if($ts=='') - { - //1.12.2008 - if(mb_ereg("([0-9]{1,2}).([0-9]{1,2}).([0-9]{4})",$datum, $regs)) - $ts = mktime(0,0,0,$regs[2],$regs[1],$regs[3]); - - //1.12.2008 12:30 - if(mb_ereg("([0-9]{1,2}).([0-9]{1,2}).([0-9]{4}) ([0-9]{2}):([0-9]{2})",$datum, $regs)) - $ts = mktime($regs[4],$regs[5],0,$regs[2],$regs[1],$regs[3]); - - //1.12.2008 12:30:15 - if(mb_ereg("([0-9]{1,2}).([0-9]{1,2}).([0-9]{4}) ([0-9]{2}):([0-9]{2}):([0-9]{2})",$datum, $regs)) - $ts = mktime($regs[4],$regs[5],$regs[6],$regs[2],$regs[1],$regs[3]); - } - - if($ts=='' && !$strict) - { - $ts = strtotime($datum); - - if(!$ts || $ts==-1) - { - //wenn strtotime fehlschlaegt liefert diese -1 zurueck, ab php5.1.0 jedoch false - $error = true; - } - } - - if($ts!='' && !$error) - return date($format, $ts); - - return false; - } -} +, + * Andreas Oesterreicher and + * Rudolf Hangl . + */ + +class datum +{ + public $ts_day=86400; // Timestamp eines Tages + + /** + * Konstruktor + * + */ + public function __construct() + { + } + + /** + * Liefert einen UNIX Timestamp von einem String im + * Format "31.12.2007 14:30" + */ + public function mktime_datumundzeit($datumundzeit) + { + if(mb_ereg("([0-9]{2}).([0-9]{2}).([0-9]{4}) ([0-9]{2}):([0-9]{2})",$datumundzeit, $regs)) + return mktime($regs[4],$regs[5],0,$regs[2],$regs[1],$regs[3]); + else + { + $this->errormsg = 'Falsches Datumsformat'; + return false; + } + } + + /** + * Liefert einen UNIX Timestamp von einem String im + * Format "31.12.2007" + */ + public function mktime_datum($datum) + { + if(mb_ereg("([0-9]{2}).([0-9]{2}).([0-9]{4})",$datum, $regs)) + { + return mktime(0,0,0,$regs[2],$regs[1],$regs[3]); + } + else + { + $this->errormsg = 'Falsches Datumsformat'; + return false; + } + } + + /** + * Liefert einen UNIX Timestamp von einem Datum im + * ISO-Format "2007-01-31" + */ + public function mktime_fromdate($datum) + { + if(mb_ereg("([0-9]{4})-([0-9]{2})-([0-9]{2})",$datum, $regs)) + { + return mktime(0,0,0,$regs[2],$regs[3],$regs[1]); + } + else + { + $this->errormsg = 'Falsches Datumsformat'; + return false; + } + } + + /** + * Liefert einen UNIX Timestamp von einem String im + * Format "2007-01-31 14:30:12" + */ + public function mktime_fromtimestamp($timestamp) + { + if(mb_ereg("([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})",$timestamp, $regs)) + { + return mktime($regs[4],$regs[5],$regs[6],$regs[2],$regs[3],$regs[1]); + } + else + { + $this->errormsg = 'Falsches Datumsformat'; + return false; + } + } + + /** + * Springt von einen UNIX Timestamp ($datum) $wochen nach vor bzw. hinten + */ + public function jump_week($datum, $wochen) + { + $stunde_vor=date("G",$datum); + // Eine Woche sind 604800 Sekunden + $datum+=604800*$wochen; + $stunde_nach=date("G",$datum); + if ($stunde_nach!=$stunde_vor) + $datum+=3600; + return $datum; + } + + /** + * Springt von einen UNIX Timestamp ($datum) $days nach vor bzw. hinten + */ + public function jump_day($datum, $days) + { + $stunde_vor=date("G",$datum); + // Ein Tag sind 86400 Sekunden + $datum+=86400*$days; + $stunde_nach=date("G",$datum); + if ($stunde_nach!=$stunde_vor) + $datum+=3600; + return $datum; + } + + /** + * Konvertiert das ISO Datumsformat (YYYY-MM-DD) + * nach (DD.MM.YYYY) + */ + public function convertISODate($datum) + { + return (mb_strlen($datum)>0?date('d.m.Y',strtotime($datum)):''); + } + + + /** + * Prueft Uhrzeit auf Gueltigkeit (HH:MM:SS) + * @return true wenn ok, false wenn falsches Format + */ + public function checkUhrzeit($uhrzeit) + { + if(mb_ereg("([0-9]{2}):([0-9]{2})(:([0-9]{2}))?$",$uhrzeit)) + return true; + else + return false; + } + + /** + * Prueft ob das Datum im Format dd.mm.YYYY oder YYYY-mm-dd ist + * @return true wenn ok, false wenn falsches Format + */ + public function checkDatum($datum) + { + if(mb_ereg("([0-9]{4})-([0-9]{2})-([0-9]{2})$",$datum) || mb_ereg("([0-9]{2}).([0-9]{2}).([0-9]{4})$",$datum)) + return true; + else + return false; + } + + + +#------------------------------------------------------------------------------------------------ + /** + * Prueft und Liefert ein Datum im angegeben Format + * fuer die Formatierung wird die Funktion formatDatum verwendet + * @param $datum + * @param $format + * @param $strict wenn das Datum aus einem Suchfeld komment, dann strict auf TRUE setzen da sonst + * Eintraege wie zB 'last Monday' oder 'a' auch in ein Datum umgewandelt werden. + * @return Formatierten Timestamp wenn ok, false im Fehlerfall + */ + function checkformatDatum($datum, $format='Y-m-d H:i:s', $strict=false) + { + + @list($day, $month, $year) = @explode(".", $datum); + if (@checkdate($month, $day, $year)) + return $this->formatDatum($datum, $format, $strict); + @list($day, $month, $year) = @explode("-", $datum); + if (@checkdate($month, $day, $year)) + return $this->formatDatum($datum, $format, $strict); + @list($year, $month, $day) = @explode(".", $datum); + if (@checkdate($month, $day, $year)) + return $this->formatDatum($datum, $format, $strict); + @list($year, $month, $day) = @explode("-", $datum); + if (@checkdate($month, $day, $year)) + return $this->formatDatum($datum, $format, $strict); + + if (strlen($datum)==6) + { + $year="20".substr($datum,0,2); + $month=substr($datum,2,2); + $day=substr($datum,4,2); + if (@checkdate($month, $day, $year)) + return $this->formatDatum($datum, $format, $strict); + } + else if (strlen($datum)==8) + { + $year=substr($datum,0,4); + $month=substr($datum,4,2); + $day=substr($datum, 6,2); + if (@checkdate($month, $day, $year)) + return $this->formatDatum($datum, $format, $strict); + + $year=substr($datum,5,4); + $month=substr($datum,3,2); + $day=substr($datum, 0,2); + if (@checkdate($month, $day, $year)) + return $this->formatDatum($datum, $format, $strict); + } + return false; + } + + + /** + * Liefert ein Datum im angegeben Format + * ToDo: Liefert aktuellen Timestamp wenn Sonderzeichen uebergeben werden + * zB '---' + * @param $datum + * @param $format + * @param $strict wenn das Datum aus einem Suchfeld komment, dann strict auf TRUE setzen da sonst + * Eintraege wie zB 'last Monday' oder 'a' auch in ein Datum umgewandelt werden. + * @return Formatierten Timestamp wenn ok, false im Fehlerfall + */ + public function formatDatum($datum, $format='Y-m-d H:i:s', $strict=false) + { + if(trim($datum)=='') + return ''; + + $ts=''; + $error=false; + + //2008-12-31 + if(mb_ereg("([0-9]{4})-([0-9]{2})-([0-9]{2})",$datum, $regs)) + $ts = mktime(0,0,0,$regs[2],$regs[3],$regs[1]); + + //2008-12-31 12:30 + if(mb_ereg("([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2})",$datum, $regs)) + $ts = mktime($regs[4],$regs[5],0,$regs[2],$regs[3],$regs[1]); + + //2008-12-31 12:30:15 + if(ereg("([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})",$datum, $regs)) + $ts = mktime($regs[4],$regs[5],$regs[6],$regs[2],$regs[3],$regs[1]); + + if($ts=='') + { + //1.12.2008 + if(mb_ereg("([0-9]{1,2}).([0-9]{1,2}).([0-9]{4})",$datum, $regs)) + $ts = mktime(0,0,0,$regs[2],$regs[1],$regs[3]); + + //1.12.2008 12:30 + if(mb_ereg("([0-9]{1,2}).([0-9]{1,2}).([0-9]{4}) ([0-9]{2}):([0-9]{2})",$datum, $regs)) + $ts = mktime($regs[4],$regs[5],0,$regs[2],$regs[1],$regs[3]); + + //1.12.2008 12:30:15 + if(mb_ereg("([0-9]{1,2}).([0-9]{1,2}).([0-9]{4}) ([0-9]{2}):([0-9]{2}):([0-9]{2})",$datum, $regs)) + $ts = mktime($regs[4],$regs[5],$regs[6],$regs[2],$regs[1],$regs[3]); + } + + if($ts=='' && !$strict) + { + $ts = strtotime($datum); + + if(!$ts || $ts==-1) + { + //wenn strtotime fehlschlaegt liefert diese -1 zurueck, ab php5.1.0 jedoch false + $error = true; + } + } + + if($ts!='' && !$error) + return date($format, $ts); + + return false; + } + +} ?> \ No newline at end of file