nntp2http.com
Posting
Suche
Optionen
Hilfe & Kontakt

Re: Nervige Sommerzeit

Von: B. Frank (son.hess@gmx.de) [Profil]
Datum: 26.02.2008 17:25
Message-ID: <fq1egq$bf$01$1@news.t-online.com>
Newsgroup: de.comp.lang.php.misc
Ich hab gerade mal nach Datetime gegoogelt, da habe ich für die Differenz
von zwei Datum´s unteren Code gefunden. Ich muss sagen, ganz schön aufwendig
für diese  einfache Aufgabe! Gehts nicht besser?

<?php

$diff=new day_difference('01.01.2900','01.01.3000',new
DateTimeZone('Europe/Berlin'));
echo $diff->result().'<br />';

$diff=new
day_difference('1000-01-01T15:19:21+00:00','1100-01-01T15:19:21+00:00',new
DateTimeZone('Europe/Berlin'));
echo $diff->result().'<br />';;

// Geht auch negativ, d.h. wieviele tage sind vergangen
$diff=new day_difference('Fri, 22 Dec 2000 16:01:07 +0200','Thu, 21 Dec 2000
16:01:07 +0200',new DateTimeZone('Europe/Berlin'));
echo $diff->result();

class day_difference
{
protected $startdate;
protected $enddate;
protected $result;

public function __construct($startdate,$enddate,datetimezone
$datetimezone=NULL)
{
$this->startdate=new DateTime($startdate,$datetimezone);
$this->enddate=new DateTime($enddate,$datetimezone);

}

public function result()
{
if(isset($this->result)) return $this->result;

$this->result=($this->enddate->format('z')+1)-($this->startdate->format('z'
)+1);
if($this->startdate->format('Y')!==$this->enddate->format('Y'))
{
$yeardifference=$this->enddate->format('Y')-$this->startdate->format('Y');
$this->result+=$yeardifference*365;
}

return $this->result;
}
}

?>



[ Auf dieses Posting antworten ]

Antworten