summaryrefslogtreecommitdiff
path: root/app/Libraries/Zeitumstellung.php
blob: 17daa0844ee1d2a22e0370f47abc35ec0ed1bbe0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
namespace App\Libraries;
use App\Libraries\Zeitumstellung;

use Carbon\Carbon;

class Zeitumstellung {

	// 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() {
		# debug
		#Carbon::setTestNow( Carbon::create(2018, 10, 28, 2) );
		#Carbon::setTestNow( Carbon::create(2020, 03, 30, 5) );

		$this->now = Carbon::now()->startOfDay();

		$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 october)

			$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 2 Uhr auf 3 Uhr vorgestellt, 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;
				$this->daysago = Carbon::parse('last sunday of october' . $this->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->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 3 Uhr auf 2 Uhr zurückgestellt, 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";
		}
	}

	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() );
	}
}