summaryrefslogtreecommitdiff
path: root/lib/prado/framework/Util/TDateTimeStamp.php
blob: 260301f003f3c4b20b2082a8194da24d9340dae0 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
/**
 * TDateTimeStamp class file.

 * @author Fabio Bas ctrlaltca[AT]gmail[DOT]com
 * @link https://github.com/pradosoft/prado
 * @copyright Copyright &copy; 2005-2016 The PRADO Group
 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
 * @package System.Util
 */

/**
 * TDateTimeStamp Class
 *
 * TDateTimeStamp Class is a wrapper to DateTime: http://php.net/manual/book.datetime.php
 * Prado implemented this class before php started shipping the DateTime extension.
 * This class is deprecated and left here only for compatibility for legacy code.
 * Please note that this class doesn't support automatic conversion from gregorian to
 * julian dates anymore.
 *
 * @author Fabio Bas ctrlaltca[AT]gmail[DOT]com
 * @package System.Util
 * @since 3.0.4
 * @deprecated since 3.2.1
 */
class TDateTimeStamp
{
	protected static $_month_normal = array("",31,28,31,30,31,30,31,31,30,31,30,31);
	protected static $_month_leaf = array("",31,29,31,30,31,30,31,31,30,31,30,31);

	/**
	 * Returns the day of the week (0=Sunday, 1=Monday, .. 6=Saturday)
	 * @param int year
	 * @param int month
	 * @param int day
	 */
	public function getDayofWeek($year, $month, $day)
	{
		$dt = new DateTime();
		$dt->setDate($year, $month, $day);
		return (int) $dt->format('w');
	}

	/**
	 * Checks for leap year, returns true if it is. No 2-digit year check. Also
	 * handles julian calendar correctly.
	 * @param float year to check
	 * @return boolean true if is leap year
	 */
	public function isLeapYear($year)
	{
		$year = $this->digitCheck($year);
		$dt = new DateTime();
		$dt->setDate($year, 1, 1);
		return (bool) $dt->format('L');
	}

	/**
	 * Fix 2-digit years. Works for any century.
	 * Assumes that if 2-digit is more than 30 years in future, then previous century.
	 * @return integer change two digit year into multiple digits
	 */
	protected function digitCheck($y)
	{
		if ($y < 100){
			$yr = (integer) date("Y");
			$century = (integer) ($yr /100);

			if ($yr%100 > 50) {
				$c1 = $century + 1;
				$c0 = $century;
			} else {
				$c1 = $century;
				$c0 = $century - 1;
			}
			$c1 *= 100;
			// if 2-digit year is less than 30 years in future, set it to this century
			// otherwise if more than 30 years in future, then we set 2-digit year to the prev century.
			if (($y + $c1) < $yr+30) $y = $y + $c1;
			else $y = $y + $c0*100;
		}
		return $y;
	}

	public function get4DigitYear($y)
	{
		return $this->digitCheck($y);
	}

	/**
	 * @return integer get local time zone offset from GMT
	 */
	public function getGMTDiff($ts=false)
	{
		$dt = new DateTime();
		if($ts)
			$dt->setTimeStamp($ts);
		else
		 	$dt->setDate(1970, 1, 2);

		return (int) $dt->format('Z');
	}

	/**
	 * @return array an array with date info.
	 */
	function parseDate($txt=false)
	{
		if ($txt === false) return getdate();

		$dt = new DateTime($txt);

		return array(
			'seconds' => (int) $dt->format('s'),
			'minutes' => (int) $dt->format('i'),
			'hours' => (int) $dt->format('G'),
			'mday' => (int) $dt->format('j'),
			'wday' => (int) $dt->format('w'),
			'mon' => (int) $dt->format('n'),
			'year' => (int) $dt->format('Y'),
			'yday' => (int) $dt->format('z'),
			'weekday' => $dt->format('l'),
			'month' => $dt->format('F'),
			0 => (int) $dt->format('U'),
			);
	}

	/**
	 * @return array an array with date info.
	 */
	function getDate($d=false,$fast=false)
	{
		if ($d === false) return getdate();

		$dt = new DateTime();
		$dt->setTimestamp($d);

		return array(
			'seconds' => (int) $dt->format('s'),
			'minutes' => (int) $dt->format('i'),
			'hours' => (int) $dt->format('G'),
			'mday' => (int) $dt->format('j'),
			'wday' => (int) $dt->format('w'),
			'mon' => (int) $dt->format('n'),
			'year' => (int) $dt->format('Y'),
			'yday' => (int) $dt->format('z'),
			'weekday' => $dt->format('l'),
			'month' => $dt->format('F'),
			0 => (int) $dt->format('U'),
			);
	}

	/**
	 * @return boolean true if valid date, semantic check only.
	 */
	public function isValidDate($y,$m,$d)
	{
		if ($this->isLeapYear($y))
			$marr =& self::$_month_leaf;
		else
			$marr =& self::$_month_normal;

		if ($m > 12 || $m < 1) return false;

		if ($d > 31 || $d < 1) return false;

		if ($marr[$m] < $d) return false;

		if ($y < 1000 && $y > 3000) return false;

		return true;
	}

	/**
	 * @return string formatted date based on timestamp $d
	 */
	function formatDate($fmt,$ts=false,$is_gmt=false)
	{
		$dt = new DateTime();
		if($is_gmt)
			$dt->setTimeZone(new DateTimeZone('UTC'));
		$dt->setTimestamp($ts);

		return $dt->format($fmt);
	}

	/**
	 * @return integer|float a timestamp given a local time
     */
	function getTimeStamp($hr,$min,$sec,$mon=false,$day=false,$year=false,$is_gmt=false)
	{
		$dt = new DateTime();
		if($is_gmt)
			$dt->setTimeZone(new DateTimeZone('UTC'));
		$dt->setDate($year!==false ? $year : date('Y'),
			$mon!==false ? $mon : date('m'),
			$day!==false ? $day : date('d'));
		$dt->setTime($hr, $min, $sec);
		return (int) $dt->format('U');
	}
}