|
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( $year ) ); |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
protected function createFromGregorian( $year, $month, $day ) { |
|
51
|
|
|
$this->m_date = ( self::GREGORIAN_EPOCH - 1 ) + |
|
52
|
|
|
( 365 * ( $year - 1 ) ) + |
|
53
|
|
|
floor( ( $year - 1 ) / 4 ) + |
|
54
|
|
|
( - floor( ( $year - 1 ) / 100 ) ) + |
|
55
|
|
|
floor( ( $year - 1 ) / 400 ) + |
|
56
|
|
|
floor( ( ( ( 367 * $month ) - 362 ) / 12 ) + |
|
57
|
|
|
( ( $month <= 2 ) ? 0 : |
|
58
|
|
|
( SRFCHistoricalDate::leap_gregorian( $year ) ? - 1 : - 2 ) |
|
|
|
|
|
|
59
|
|
|
) + $day ); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
protected function createFromJulian( $year, $month, $day ) { |
|
63
|
|
|
|
|
64
|
|
|
/* Adjust negative common era years to the zero-based notation we use. */ |
|
65
|
|
|
if ( $year < 1 ) { |
|
66
|
|
|
$year++; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/* Algorithm as given in Meeus, Astronomical Algorithms, Chapter 7, page 61 */ |
|
70
|
|
|
if ( $month <= 2 ) { |
|
71
|
|
|
$year--; |
|
72
|
|
|
$month += 12; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$this->m_date = ( ( floor( ( 365.25 * ( $year + 4716 ) ) ) + |
|
76
|
|
|
floor( ( 30.6001 * ( $month + 1 ) ) ) + |
|
77
|
|
|
$day ) - 1524.5 ); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function getDayOfWeek() { |
|
81
|
|
|
return ( floor( $this->m_date + 1.5 ) % 7 ); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
static function daysInMonth( $year, $month ) { |
|
|
|
|
|
|
85
|
|
|
if ( $month == 4 || $month == 6 || $month == 9 || $month == 11 ) |
|
86
|
|
|
return 30; |
|
87
|
|
|
if ( $month == 2 ) |
|
88
|
|
|
return ( SRFCHistoricalDate::leap_jul_greg( $year ) ) ? 29 : 28; |
|
|
|
|
|
|
89
|
|
|
return 31; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|
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.