diff options
Diffstat (limited to 'inc')
| -rw-r--r-- | inc/Zeitumstellung.php | 122 | ||||
| -rw-r--r-- | inc/config.php | 8 | ||||
| -rw-r--r-- | inc/ical.php | 39 |
3 files changed, 169 insertions, 0 deletions
diff --git a/inc/Zeitumstellung.php b/inc/Zeitumstellung.php new file mode 100644 index 0000000..57476fe --- /dev/null +++ b/inc/Zeitumstellung.php @@ -0,0 +1,122 @@ +<?php +require_once __DIR__ . '/../vendor/autoload.php'; + +use Carbon\Carbon; + +class Zeitumstellung { + + // Sommerzeit | Winterzeit + public $season; + // full date of the next time change + public $date; + public $dateText; + // number of days since last time change + public $daysago; + // number of days until next time change. if zero, time change is today! + public $daysuntil; + + public $description; + public $descriptionHTML; + + public $day; + public $month; + public $monthText; + public $year; + + public $now; + + private $tc_march; + private $tc_oct; + + function __construct($setNow = null) { + # debug + #Carbon::setTestNow( Carbon::create(2018, 10, 28, 2) ); + #Carbon::setTestNow( Carbon::create(2020, 03, 30, 5) ); + + if ( is_null($setNow) ) { + $this->now = Carbon::now()->startOfDay(); + } else { + $this->now = $setNow; + } + + $this->year = $this->now->year; + + # detect if next time change is next year + if ( $this->now->gt( Carbon::parse('last sunday of october')->startOfDay() ) ){ + $this->year = $this->year+1; + } + + $this->tc_march = Carbon::parse('last sunday of march ' . $this->year )->startOfDay(); + $this->tc_oct = Carbon::parse('last sunday of october ' . $this->year)->startOfDay(); + + if ( $this->now->lte($this->tc_march) ){ + # next time change is in march this year, (last sunday in march) + + $this->season = "Sommerzeit"; + + $this->date = $this->tc_march; + $this->description = 'Die Uhr wird von 2 Uhr auf 3 Uhr vorgestellt, sodass wir 1 Stunde weniger schlafen.'; + $this->descriptionHTML = 'Die Uhr wird von <b>2 Uhr auf 3 Uhr vorgestellt</b>, sodass wir <b>1 Stunde weniger</b> schlafen.'; + + # detect if last time change was last year + if ( $this->now->lt( Carbon::parse('last sunday of march ') ) ) { + #$this->year = $this->now->year - 1; + $year = $this->now->year - 1; + $this->daysago = Carbon::parse('last sunday of october' . $year)->diffInDays( $this->now ); + } else { + $this->daysago = $this->tc_oct->diffInDays( $this->now ); + } + + } else { + # next time change is in october this year, (last sunday in october) + + $this->season = "Winterzeit"; + + $this->date = $this->tc_oct; + $this->description = 'Die Uhr wird von 3 Uhr auf 2 Uhr zurückgestellt, sodass wir 1 Stunde länger schlafen.'; + $this->descriptionHTML = 'Die Uhr wird von <b>3 Uhr auf 2 Uhr zurückgestellt</b>, sodass wir <b>1 Stunde länger</b> schlafen.'; + + $this->daysago = $this->tc_march->diffInDays( $this->now ); + } + + $this->dateText = $this->date->toDateString(); + + $this->daysuntil = $this->date->diffInDays($this->now); + + $this->day = $this->date->day; + $this->month = $this->date->month; + $this->year = $this->date->year; + + if ( 3 == $this->month ) { + $this->monthText = "März"; + } else { + $this->monthText = "Oktober"; + } + } + + /** + * Returns an instance of the object with the next time change after the current one. + */ + function next(){ + return new Zeitumstellung( $this->date->add(1, 'day') ); + } + + public function getData(){ + $result = array(); + + $result["date"] = $this->date->toDateString(); + $result["day"] = $this->date->day; + $result["month"] = $this->date->month; + $result["monthText"] = $this->monthText; + $result["year"] = $this->date->year; + $result["daysuntil"] = $this->daysuntil; + $result["daysago"] = $this->daysago; + $result["now"] = $this->now->toDateString(); + + return $result; + } + + public function getJSON(){ + return json_encode( $this->getData() ); + } +} diff --git a/inc/config.php b/inc/config.php new file mode 100644 index 0000000..ab5b7ff --- /dev/null +++ b/inc/config.php @@ -0,0 +1,8 @@ +<?php + +date_default_timezone_set("Europe/Berlin"); + +define("APP_NAME", "Zeitumstellung"); +define("APP_BASE", "zeitumstellung.iamfabulous.de"); +define("APP_SCHEME", "https://"); +define("APP_URL", APP_SCHEME . APP_BASE); diff --git a/inc/ical.php b/inc/ical.php new file mode 100644 index 0000000..7cc30e9 --- /dev/null +++ b/inc/ical.php @@ -0,0 +1,39 @@ +<?php +require_once __DIR__ . '/../vendor/autoload.php'; + +use Eluceo\iCal\Component\Calendar; +use Eluceo\iCal\Component\Event; + +function iCalFeed() { + + $vCalendar = new Calendar(APP_BASE); + $vCalendar->setName(APP_NAME); + $vCalendar->setTimezone('Europe/Berlin'); + + $tc = new Zeitumstellung(); + + for ( $i=0; $i<3; $i++ ) { + + $vEvent = new Event(); + $vEvent + ->setDtStart(new \DateTime($tc->dateText)) + ->setDtEnd(new \DateTime($tc->dateText)) + ->setNoTime(true) + ->setSummary('Zeitumstellung') + ->setUrl( APP_URL ) + ->setDescription($tc->description) + ->setDescriptionHTML($tc->descriptionHTML); + + $vCalendar->addComponent($vEvent); + + $tc = $tc->next(); + } + + header('Content-Type: text/calendar; charset=utf-8'); + + if ( '0' != $_REQUEST['download'] ) { + header('Content-Disposition: attachment; filename="zeitumstellung.ics"'); + } + + echo $vCalendar->render(); +} |
