1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* SRFC_HistoricalDate.php |
5
|
|
|
* |
6
|
|
|
* This code is lifted from Terry Hurlbut's 'SMW_DV_HxDate.php' class; |
7
|
|
|
* that code was itself adapted from the Fourmilab Calendar Converter |
8
|
|
|
* Javascripts by John Walker, who wrote them in 1999 and released them |
9
|
|
|
* to the public domain. |
10
|
|
|
* |
11
|
|
|
* The internal value, unlike that of the standard SMW Date type, is a |
12
|
|
|
* 64-bit PHP float. The characteristic gives the days since |
13
|
|
|
* the epoch. |
14
|
|
|
* |
15
|
|
|
* Technically, the Julian calendar is valid only beginning January 1, 45 BC, |
16
|
|
|
* when Julius Caesar established it as per a formal Senatus consultum. But |
17
|
|
|
* currently this is the only calendar currently projectible to earlier times; |
18
|
|
|
* therefore Julian dates are valid for any year in the Julian Period. |
19
|
|
|
* |
20
|
|
|
* @author Terry A. Hurlbut |
21
|
|
|
* @author Yaron Koren |
22
|
|
|
*/ |
23
|
|
|
class SRFCHistoricalDate { |
24
|
|
|
|
25
|
|
|
const GREGORIAN_EPOCH = 1721425.5; // equivalent to 1 AD |
26
|
|
|
|
27
|
|
|
protected $m_date; // the Julian day |
28
|
|
|
|
29
|
|
|
function create( $year, $month, $day ) { |
|
|
|
|
30
|
|
|
if ( $year < 1582 || |
31
|
|
|
( $year == 1582 && ( $month < 10 || ( $month == 10 && $day < 15 ) ) ) ) { |
32
|
|
|
$this->createFromJulian( $year, $month, $day ); |
33
|
|
|
} else { |
34
|
|
|
$this->createFromGregorian( $year, $month, $day ); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
static protected function leap_gregorian( $year ) { |
39
|
|
|
return ( ( $year % 4 ) == 0 ) && ( !( ( ( $year % 100 ) == 0 ) && ( ( $year % 400 ) != 0 ) ) ); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
static protected function leap_julian( $year ) { |
43
|
|
|
return ( ( $year % 4 ) == ( ( $year > 0 ) ? 0 : 3 ) ); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
static protected function leap_jul_greg( $year ) { |
47
|
|
|
return ( ( $year < 1582 ) ? SRFCHistoricalDate::leap_julian( $year ) : SRFCHistoricalDate::leap_gregorian( |
|
|
|
|
48
|
|
|
$year |
49
|
|
|
) ); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function createFromGregorian( $year, $month, $day ) { |
53
|
|
|
$this->m_date = ( self::GREGORIAN_EPOCH - 1 ) + |
54
|
|
|
( 365 * ( $year - 1 ) ) + |
55
|
|
|
floor( ( $year - 1 ) / 4 ) + |
56
|
|
|
( -floor( ( $year - 1 ) / 100 ) ) + |
57
|
|
|
floor( ( $year - 1 ) / 400 ) + |
58
|
|
|
floor( |
59
|
|
|
( ( ( 367 * $month ) - 362 ) / 12 ) + |
60
|
|
|
( ( $month <= 2 ) ? 0 : |
61
|
|
|
( SRFCHistoricalDate::leap_gregorian( $year ) ? -1 : -2 ) |
|
|
|
|
62
|
|
|
) + $day |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function createFromJulian( $year, $month, $day ) { |
67
|
|
|
|
68
|
|
|
/* Adjust negative common era years to the zero-based notation we use. */ |
69
|
|
|
if ( $year < 1 ) { |
70
|
|
|
$year++; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/* Algorithm as given in Meeus, Astronomical Algorithms, Chapter 7, page 61 */ |
74
|
|
|
if ( $month <= 2 ) { |
75
|
|
|
$year--; |
76
|
|
|
$month += 12; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$this->m_date = ( ( floor( ( 365.25 * ( $year + 4716 ) ) ) + |
80
|
|
|
floor( ( 30.6001 * ( $month + 1 ) ) ) + |
81
|
|
|
$day ) - 1524.5 ); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function getDayOfWeek() { |
85
|
|
|
return ( floor( $this->m_date + 1.5 ) % 7 ); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
static function daysInMonth( $year, $month ) { |
|
|
|
|
89
|
|
|
if ( $month == 4 || $month == 6 || $month == 9 || $month == 11 ) { |
90
|
|
|
return 30; |
91
|
|
|
} |
92
|
|
|
if ( $month == 2 ) { |
93
|
|
|
return ( SRFCHistoricalDate::leap_jul_greg( $year ) ) ? 29 : 28; |
|
|
|
|
94
|
|
|
} |
95
|
|
|
return 31; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.