Passed
Push — master ( 6e04b4...6a6782 )
by Samson
02:22
created

Calender::jdToEthiopian()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 20
rs 9.4285
cc 3
eloc 14
nc 3
nop 2
1
<?php
2
3
namespace Andegna;
4
5
/**
6
 * Class Calender <br />
7
 * A set of functions to convert ethiopian date to JDN and vise versa.
8
 */
9
class Calender
10
{
11
    /**
12
     * Return a true if the year is a leap year based on the ethiopian calendar.
13
     *
14
     * @param int $year the ethiopian date to be checked
15
     *
16
     * @return bool
17
     */
18
    public static function isEthiopianLeapYear($year)
19
    {
20
        if (!is_int($year)) {
21
            trigger_error(
22
                'isEthiopianLeapYear() expects parameter 1 to be numeric. '.
23
                gettype($year).' given',
24
                E_USER_WARNING
25
            );
26
        }
27
28
        return ($year + 1) % 4 == 0;
29
    }
30
31
    /**
32
     * Returns true if the <code>$month, $day and $year</code> passed
33
     * are a valid date based on the ethiopian calendar.
34
     *
35
     * @param int $month Ethiopian month
36
     * @param int $day   Ethiopian day
37
     * @param int $year  Ethiopian year (negative for AD)
38
     *
39
     * @return bool
40
     */
41
    public static function ethiopianCheckDate($month, $day, $year)
42
    {
43
        return
44
            // validate all
45
            is_int($month) && is_int($day) && is_int($year) &&
46
            // true if the day is btn 1 - 30
47
            ($day <= 30 && $day >= 1) &&
48
            // true if the month is btn 1 - 13
49
            ($month <= 13 && $month >= 1) &&
50
            // true if the month is 13 then day is btn 1 - 6
51
            ($month == 13 ? $day <= 6 : true) &&
52
            // true if the month is 13 & day is 6 then year must be leap year
53
            ($month == 13 && $day == 6 ?
54
                self::isEthiopianLeapYear($year) : true);
55
    }
56
57
    /**
58
     * Returns the julian date number representation of the
59
     * given ethiopian date.
60
     *
61
     * @param int $month Ethiopian month
62
     * @param int $day   Ethiopian day
63
     * @param int $year  Ethiopian year (negative for AD)
64
     *
65
     * @return int
66
     */
67
    public static function ethiopianToJd($month = 1, $day = 1, $year = 2000)
68
    {
69
        if (!self::ethiopianCheckDate($month, $day, $year)) {
70
            trigger_error(
71
                'ethiopianToJd() expects the date to be valid.'.
72
                ' check ethiopianCheckDate() first',
73
                E_USER_WARNING
74
            );
75
        }
76
77
        return (1723856 + 365) +
78
        365 * ($year - 1) +
79
        (int) ($year / 4) +
80
        30 * $month +
81
        $day - 31;
82
    }
83
84
    /**
85
     * Returns the ethiopian date string which is represented by
86
     * the passed jdn <br />.
87
     *
88
     * @param int  $jdn   the Julian Date Number
89
     * @param bool $array if this is true the function returns
90
     *                    the day,month and year in associative array.
91
     *
92
     * @return array|string
93
     */
94
    public static function jdToEthiopian($jdn, $array = false)
95
    {
96
        if (!is_int($jdn)) {
97
            trigger_error(
98
                'jdToEthiopian() expects parameter 1 to be numeric. '.
99
                gettype($jdn).' given',
100
                E_USER_WARNING
101
            );
102
103
            return self::jdToEthiopian(self::ethiopianToJd());
104
        }
105
        $r = (($jdn - 1723856) % 1461);
106
        $n = ($r % 365) + 365 * (int) ($r / 1460);
107
        $year = 4 * (int) (($jdn - 1723856) / 1461) +
108
            (int) ($r / 365) - (int) ($r / 1460);
109
        $month = (int) ($n / 30) + 1;
110
        $day = ($n % 30) + 1;
111
112
        return ($array ? compact('day', 'month', 'year') : "$month/$day/$year");
113
    }
114
}
115