본문 바로가기

프로그래밍 공부/PHP

PHP - 음력과 양력을 구하는 소스

lunar_solar_base.php

더보기
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
<?
    // 적용 가능 기간: -9999 ~ +9999년
 
    class LunarSolarBase
    {
        const MONTH        = array (
            021355428436449886335108366130578152958,
            175471198077220728243370265955288432310767,
            332928354903376685398290419736441060462295,
            483493504693525949
        );
        
        // 십간 상수정의
        const KOR_GAN    = array ('갑''을''병''정''무''기''경''신''임''계');
        const HAN_GAN    = array ('甲''乙''丙''丁''戊''己''庚''辛''壬''癸');
 
        // 십이지 상수정의
        const KOR_JI    = array ('자''축''인''묘''진''사''오''미''신''유''술''해');
        const HAN_JI    = array ('子''丑''寅''卯''辰''巳''午''未''申''酉''戌''亥');
 
        // 띠 상수정의
        const ZODIAC    = array ('쥐''소''호랑이''토끼''용''뱀''말''양''원숭이''닭''개''돼지');
 
        // 병자년 경인월 신미일 기해시 입춘 데이터
        const UNIT_YEAR        = 1996;
        const UNIT_MONTH    = 2;
        const UNIT_DAY        = 4;
        const UNIT_HOUR        = 22;
        const UNIT_MIN        = 8;
        const UNIT_SEC        = 0;
 
        // 병자년 데이터
        const UY_GAN        = 2;
        const UY_JI            = 0;
        const UY_SU            = 12;
 
        // 경인년 데이터
        const UM_GAN        = 6;
        const UM_JI            = 2;
        const UM_SU            = 26;
 
        // 신미일 데이터
        const UH_GAN        = 5;
        const UH_JI            = 11;
        const UH_SU            = 35;
 
        // 정원 초하루 합삭 시간
        const UNIT_M_YEAR    = 1996;
        const UNIT_M_MONTH    = 2;
        const UNIT_M_DAY    = 19;
        const UNIT_M_HOUR    = 8;
        const UNIT_M_MIN    = 30;
        const UNIT_M_SEC    = 0;
        const MOON_LENGTH    = 42524;
 
        // 절기 데이터
        const KOR_MONTH_STR    = array (
            '입춘''우수''경칩''춘분''청명''곡우',
            '입하''소만''망종''하지''소서''대서',
            '입추''처서''백로''추분''한로''상강',
            '입동''소설''대설''동지''소한''대한',
            '입춘'
        );
        const HAN_MONTH_STR = array (
            '立春''雨水''驚蟄''春分''淸明''穀雨',
            '立夏''小滿''芒種''夏至''小暑''大暑',
            '立秋''處暑''白露''秋分''寒露''霜降',
            '立冬''小雪''大雪''冬至''小寒''大寒',
            '立春'
        );
 
        // 60간지 데이터
        const KOR_GANJI        = array (
            '갑자''을축''병인''정묘''무진''기사''경오''신미''임신''계유''갑술''을해',
            '병자''정축''무인''기묘''경진''신사''임오''계미''갑신''을유''병술''정해'
            '무자''기축''경인''신묘''임진''계사''갑오''을미''병신''정유''무술''기해'
            '경자''신축''임인''계묘''갑진''을사''병오''정미''무신''기유''경술''신해',
            '임자''계축''갑인''을묘''병진''정사''무오''기미''경신''신유''임술''계해'
        );
        const HAN_GANJI        = array (
            '甲子','乙丑','丙寅','丁卯','戊辰','己巳','庚午','辛未','壬申','癸酉','甲戌','乙亥',
            '丙子','丁丑','戊寅','己卯','庚辰','辛巳','壬午','癸未','甲申','乙酉','丙戌','丁亥',
            '戊子','己丑','庚寅','辛卯','壬辰','癸巳','甲午','乙未','丙申','丁酉','戊戌','己亥',
            '庚子','辛丑','壬寅','癸卯','甲辰','乙巳','丙午','丁未','戊申','己酉','庚戌','辛亥',
            '壬子','癸丑','甲寅','乙卯','丙辰','丁巳','戊午','己未','庚申','辛酉','壬戌','癸亥'
        );
 
        // 요일 데이터
        const KOR_WEEK        = array ('일','월','화','수','목','금','토');
        const HAN_WEEK        = array ('日','月','火','水','木','金','土');
 
        // 28일 데이터
        const KOR_28_DAYS    = array (
            '각''항''저''방''심''미''기',
            '두''우''녀''허''위''실''벽',
            '규''수''위''묘''필''자''삼',
            '정''귀''류''성''장''익''진'
        );
        const HAN_28_DAYS    = array (
            '角','亢','氐','房','心','尾','箕',
            '斗','牛','女','虛','危','室','壁',
            '奎','婁','胃','昴','畢','觜','參',
            '井','鬼','柳','星','張','翼','軫'
        );
 
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
        // 정수 몫을 반환
        public static function get_integer_share ($value_1$value_2)
        {
            return (int) ($value_1 / $value_2);
        }
 
        // 특정년의 1월 1일부터 해당 날짜(date)까지의 날짜의 수
        // date = $year, $month, $day
        public static function count_day_from_first_day ($year$month$day)
        {
            $day_count = $i = 0;
 
            for($i = 1$i < $month$i ++)
            {
                $day_count += 31;
                if($i == 2 || $i == 4 || $i == 6 || $i == 9 || $i == 11)
                    $day_count --;
 
                if($i == 2)
                {
                    $day_count -= 2;
                    if(($year % 4== 0)
                        $day_count ++;
                    if(($year % 100== 0)
                        $day_count --;
                    if(($year % 400== 0)
                        $day_count ++;
                    if(($year % 4000== 0)
                        $day_count --;
                }
            }
            $day_count += $day;
 
            return $day_count;
        }
 
        // 특정 날짜($start_date)부터 특정 날짜($end_date)까지의 일수를 계산
        // start_date = $start_year, $start_month, $start_day
        // end_date = $end_year, $end_month, $end_day
        public static function count_day_from_start_to_end ($start_year$start_month$start_day$end_year$end_month$end_day)
        {
            $a = $b = $c = $d = $e = $f = $g = $h = $i = $day_count_result = 0;
 
            if($y2 > $y1)
            {
                $a = self::count_day_from_first_day ($start_year$start_month$start_day);
                $c = self::count_day_from_first_day ($start_year1231);
                $b = self::count_day_from_first_day ($end_year$end_month$end_day);
                $d = $start_year;
                $e = $end_year;
                $f = -1;
            }
            else
            {
                $a = self::count_day_from_first_day ($end_year$end_month$end_day);
                $c = self::count_day_from_first_day ($end_year1231);
                $b = self::count_day_from_first_day ($start_year$start_month$start_day);
                $d = $end_year;
                $e = $start_year;
                $f = 1;
            }
 
            if($end_year == $start_year)
                $day_count_result = $b - $a;
            else
            {
                $day_count_result = $c - $a;
                $g = $d + 1;
                $h = $e - 1;
 
                for($i = $g$i <= $h$i ++)
                {
                    if($i == -2000 && $h > 1990)
                    {
                        $day_count_result += 1457682;
                        $i = 1991;
                    }
                    else if($i == -1750 && $h > 1990)
                    {
                        $day_count_result += 1366371;
                        $i = 1991;
                    }
                    else if($i == -1500 && $h > 1990)
                    {
                        $day_count_result += 1275060;
                        $i = 1991;
                    }
                    else if($i == -1250 && $h > 1990)
                    {
                        $day_count_result += 1183750;
                        $i = 1991;
                    }
                    else if($i == -1000 && $h > 1990)
                    {
                        $day_count_result += 1092439;
                        $i = 1991;
                    }
                    else if($i == -750 && $h > 1990)
                    {
                        $day_count_result += 1001128;
                        $i = 1991;
                    }
                    else if($i == -500 && $h > 1990)
                    {
                        $day_count_result += 909818;
                        $i = 1991;
                    }
                    else if($i == -250 && $h > 1990)
                    {
                        $day_count_result += 818507;
                        $i = 1991;
                    }
                    else if($i == 0 && $h > 1990)
                    {
                        $day_count_result += 727197;
                        $i = 1991;
                    }
                    else if($i == 250 && $h > 1990)
                    {
                        $day_count_result += 635887;
                        $i = 1991;
                    }
                    else if($i == 500 && $h > 1990)
                    {
                        $day_count_result += 544576;
                        $i = 1991;
                    }
                    else if($i == 750 && $h > 1990)
                    {
                        $day_count_result += 453266;
                        $i = 1991;
                    }
                    else if($i == 1000 && $h > 1990)
                    {
                        $day_count_result += 361955;
                        $i = 1991;
                    }
                    else if($i == 1250 && $h > 1990)
                    {
                        $day_count_result += 270644;
                        $i = 1991;
                    }
                    else if($i == 1500 && $h > 1990)
                    {
                        $day_count_result += 179334;
                        $i = 1991;
                    }
                    else if($i == 1750 && $h > 1990)
                    {
                        $day_count_result += 88023;
                        $i = 1991;
                    }
 
                    $dis += self::count_day_from_first_day ($i1231);
                }
 
                $day_count_result += $b;
                $day_count_result *= $f;
            }
 
            return $day_count_result;
        }
 
        // start_date와 end_date사이의 시간(분)을 계산
        // start_date = $start_year, $start_month, $start_day, $start_hour, $start_min
        // end_date = $end_year, $end_month, $end_day, $end_hour, $end_min
        public static function count_min_from_start_to_end ($start_year$start_month$start_day$start_hour$start_min$end_year$end_month$end_day$end_hour$end_min)
        {
            $count_min = 0;
            
            $count_day    = self::count_day_from_start_to_end ($start_year$start_month$start_day$end_year$end_month$end_day);
            $count_min    = $count_day * 24 * 60 + ($start_hour - $end_hour* 60 + ($start_min - $end_min);
 
            return $count_min;
        }
 
        // distinct_date으로 부터 $target_min(분) 떨어진 시점의 년/월/일/시/분을 계산
        // distinct_date = $distinct_year, $distinct_month, $distinct_day, $distinct_hour, $distinct_min
        // $target_min = (int)
        public static function get_date_by_target_min ($target_min$distinct_year$distinct_month$distinct_day$distinct_hour$distinct_min)
        {
            $year = $month = $day = $hour = $min = $time = 0;
            $year = $distinct_year - self::get_integer_share ($target_min525949);
 
            if($target_min > 0)
            {
                $year += 2;
                do
                {
                    $year    --;
                    $time    = self::count_min_from_start_to_end ($distinct_year$distinct_month$distinct_day$distinct_hour$distinct_min$year1100);
                }
                while($time < $target_min);
 
                $month = 13;
                do
                {
                    $month    --;
                    $time    = self::count_min_from_start_to_end ($distinct_year$distinct_month$distinct_day$distinct_hour$distinct_min$year$month100);
                }
                while($time < $target_min);
 
                $day = 32;
                do
                {
                    $day    --;
                    $time    = self::count_min_from_start_to_end ($distinct_year$distinct_month$distinct_day$distinct_hour$distinct_min$year$month$day00);
                }
                while($time < $target_min);
 
                $hour = 24;
                do
                {
                    $hour    --;
                    $time    = self::count_min_from_start_to_end ($distinct_year$distinct_month$distinct_day$distinct_hour$distinct_min$year$month$day$hour0);
                }
                while($time < $target_min);
 
                $time    = self::count_min_from_start_to_end ($distinct_year$distinct_month$distinct_day$distinct_hour$distinct_min$year$month$day$hour0);
                $min    = $time - $target_min;
            }
            else
            {
                $year -= 2;
                do
                {
                    $year ++;
                    $time = self::count_min_from_start_to_end ($distinct_year$distinct_month$distinct_day$distinct_hour$distinct_min$year1100);
                }
                while($time >= $target_min);
 
                $year    --;
                $month    = 0;
                do
                {
                    $month ++;
                    $time = self::count_min_from_start_to_end ($distinct_year$distinct_month$distinct_day$distinct_hour$distinct_min$year$month100);
                }
                while($time >= $target_min);
 
                $month    --;
                $day    = 0;
                do
                {
                    $day    = $day + 1;
                    $time    = self::count_min_from_start_to_end ($distinct_year$distinct_month$distinct_day$distinct_hour$distinct_min$year$month$day00); 
                }
                while($time >= $target_min);
 
                $day    --;
                $hour    = -1;
                do
                {
                    $hour ++;
                    $time = self::count_min_from_start_to_end ($distinct_year$distinct_month$distinct_day$distinct_hour$distinct_min$year$month$day$hour0);
                }
                while($time >= $target_min);
 
                $hour --;
                $time = self::count_min_from_start_to_end ($uyear$umonth$uday$uhour$umin$year$month$day$hour0);
                $min = $time - $target_min;
            }
 
            return array ($year$month$day$hour$min);
        }
 
        // 그래고리력의 년/월/시/분으로 60년의 배수, 세차, 월건, 일진, 시주를 구함
        /*
        Array (
            [0] => -17    // 60년의 배수
            [1] => 29    // 60간지의 년 배열 index
            [2] => 55    // 60간지의 월 배열 index
            [3] => 11    // 60간지의 일 배열 index
            [4] => 20    // 60간지의 시 배열 index
        )
        */
        public static function get_sexagenary_by_gregorian ($solar_year$solar_month$solar_day$solar_hour$solar_min)
        {
            $count_min = self::count_min_from_start_to_end (
                self::UNIT_YEAR, self::UNIT_MONTH, self::UNIT_DAY, self::UNIT_HOUR, self::UNIT_MIN,
                $solar_year$solar_month$solar_day$solar_hour$solar_min
            );
            $count_day = self::count_day_from_start_to_end (
                self::UNIT_YEAR, self::UNIT_MONTH, self::UNIT_DAY,
                $solar_year$solar_month$solar_day
            );
 
            // 무인년(1996) 입춘부터 해당일시까지의 경과년수
            $sexagenary = self::get_integer_share ($count_min525949);
 
            if($count_min >= 0)
                $sexagenary ++;
 
            // 년주 계산
            $sexagenary_year = ($sexagenary % 60* -1;
            $sexagenary_year += 12;
        
            if($sexagenary_year < 0)
                $sexagenary_year += 60;
            else if($sexagenary_year > 59)
                $sexagenary_year -= 60;
 
            $k = $count_min % 525949;
            $k = 525949 - $k;
 
            if($k < 0)
                $k += 525949;
            else if($k >= 525949)
                $k -= 525949;
 
            for($i = 0$i <= 11$i ++)
            {
                $j = $i * 2;
                if(self::MONTH[$j<= $k && $k < self::MONTH[$j+2])
                {
                    $sexagenary_month = $i;
                }
            };
 
            // 월주 구하기
            $i = $sexagenary_month;
            $j = $sexagenary_year % 10;
            $j %= 5;
            $j = $j * 12 + 2 + $i;
            
            $sexagenary_month = $j;
 
            if($sexagenary_month > 50)
                $sexagenary_month -= 60;
 
            $sexagenary_day = $count_day % 60;
 
            // 일주 구하기
            $sexagenary_day *= -1;
            $sexagenary_day += 7;
            
            if($sexagenary_day < 0)
                $sexagenary_day += 60;
            else if($sexagenary_day > 59)
                $sexagenary_day -= 60;
 
            if(($solar_hour == 0 || $solar_hour == 1 && $solar_min < 30))
                $i = 0;
 
            else if(($solor_hour == 1 && $solor_min >= 30|| $solor_hour == 2 || ($solor_hour == 3 && $solor_min < 30))
                $i = 1;
 
            else if(($solor_hour == 3 && $solor_min >= 30|| $solor_hour == 4 || ($solor_hour == 5 && $solor_min < 30))
                $i = 2;
 
            else if(($solor_hour == 5 && $solor_min >= 30|| $solor_hour == 6 || ($solor_hour == 7 && $solor_min < 30))
                $i = 3;
 
            else if(($solor_hour == 7 && $solor_min >= 30|| $solor_hour == 8 || ($solor_hour == 9 && $solor_min < 30))
                $i = 4;
 
            else if(($solor_hour == 9 && $solor_min >= 30|| $solor_hour == 10 || ($solor_hour == 11 && $solor_min < 30))
                $i = 5;
 
            else if(($solor_hour == 11 && $solor_min >= 30|| $solor_hour == 12 || ($solor_hour == 13 && $solor_min < 30))
                $i = 6;
 
            else if(($solor_hour == 13 && $solor_min >= 30|| $solor_hour == 14 || ($solor_hour == 15 && $solor_min < 30))
                $i = 7;
 
            else if(($solor_hour == 15 && $solor_min >= 30|| $solor_hour == 16 || ($solor_hour == 17 && $solor_min < 30))
                $i = 8;
 
            else if(($solor_hour == 17 && $solor_min >= 30|| $solor_hour == 18 || ($solor_hour == 19 && $solor_min < 30))
                $i = 9;
 
            else if(($solor_hour == 19 && $solor_min >= 30|| $solor_hour == 20 || ($solor_hour == 21 && $solor_min < 30))
                $i = 10;
 
            else if(($solor_hour == 21 && $solor_min >= 30|| $solor_hour == 22 || ($solor_hour == 23 && $solor_min < 30))
                $i = 11;
 
            else if($solar_hour == 23 && $solar_min >= 30)
            {
                $sexagenary_day ++;
        
                if($solar_day == 60)
                    $sexagenary_day = 0;
                
                $i = 0;
            }
 
            $j = $sexagenary_day % 10;
            $j %= 5;
            $j = $j * 12 + $i;
            
            $sexagenary_hour = $j;
            
            return array ($sexagenary$sexagenary_year$sexagenary_month$sexagenary_day$sexagenary_hour);
        }
 
        // 그래고리력의 년/월/시/분이 들어있는 절기(season)의 이름번호, 년/월/일/시/분을 얻는다.
        public static function get_season_by_gregorian ($solar_year$solar_month$solar_day$solar_hour$solar_min)
        {
            list ($season$season_year$season_month$season_day$season_hour= self::get_sexagenary_by_gregorian (
                $solar_year$solar_month$solar_day$solar_hour$solar_min
            );
 
            $count_min = self::count_min_from_start_to_end (
                self::UNIT_YEAR, self::UNIT_MONTH, self::UNIT_DAY, self::UNIT_HOUR, self::UNIT_MIN,
                $solar_year$solar_month$solar_day$solor_hour$solar_min
            );
 
            // $k = $count_min % 525949;
            // $k = 525949 - $k;
            $k = ($count_min % 525949* -1;
 
            if($k < 0)
                $k += 525949;
            else if($k >= 525949)
                $k = $k - 525949;
 
            $i = $season_month % 12 - 2;
            if($i == -2)
                $i = 10;
            else if($i == -1)
                $i = 11;
 
            $ingi_name    = $i * 2;
            $mid_name    = $i * 2 + 1;
            $outgi_name    = $i * 2 + 2;
            
            $j = $i * 2;
            $target_min = $count_min + ($k - self::MONTH[$j]);
 
            list ($year$month$day$hour$min= self::get_date_by_target_min (
                $target_min,
                self::UNIT_YEAR,
                self::UNIT_MONTH,
                self::UNIT_DAY,
                self::UNIT_HOUR,
                self::UNIT_MIN
            );
 
            $ingi_year    = $year;
            $ingi_month    = $month;
            $ingi_day    = $day;
            $ingi_hour    = $hour;
            $ingi_min    = $min;
 
            $target_min = $count_min + ($k - self::MONTH[$j+1]);
 
            list ($year$month$day$hour$min= self::get_date_by_target_min (
                $target_min,
                self::UNIT_YEAR,
                self::UNIT_MONTH,
                self::UNIT_DAY,
                self::UNIT_HOUR,
                self::UNIT_MIN
            );
 
            $mid_year    = $year;
            $mid_month    = $month;
            $mid_day    = $day;
            $mid_hour    = $hour;
            $mid_min    = $min;
 
            $tmin = $count_min + ($k - self::MONTH[$j + 2]);
 
            list ($year$month$day$hour$min= self::get_date_by_target_min (
                $target_min,
                self::UNIT_YEAR,
                self::UNIT_MONTH,
                self::UNIT_DAY,
                self::UNIT_HOUR,
                self::UNIT_MIN
            );
 
            $outgi_year        = $year;
            $outgi_month    = $month;
            $outgi_day        = $day;
            $outgi_hour        = $hour;
            $outgi_min        = $min;
 
            return array (
                $ingi_year$ingi_month$ingi_day$ingi_hour$ingi_min,
                $mid_year$mid_month$mid_day$mid_hour$mid_min,
                $outgi_year$outgi_month$outgi_day$outgi_hour$outgi_min
            );
        }
 
        // 특정한 각도를 0도 ~ 360도 이내로 계산
        public static function get_degree_between_0_to_360 ($degree)
        {
            $degree_result = $degree;
            $i = self::get_integer_share ((int$degree_result360);
            $degree_result = $degree - ($i * 360);
 
            while($degree_result >= 360 || $degree < 0)
            {
                if($degree_result > 0)
                    $degree_result -= 360;
                else
                    $degree_result += 360;
            }
 
            return $degree_result;
        }
 
        // 1996년 기준 태양황경과 달황경의 차이
        public static function get_sun_moon_longitude_gap ($day)
        {
            // 태양황경
            $sun_celestial_longitude        = (float) ($day * 0.98564736 + 278.956807);                                                            // 평균 황경
            $sun_perihelion                    = 282.869498 + 0.00004708 * $day;                                                                    // 근일점 황경
            $sun_anomaly                    = 3.14159265358979 * ($sun_celestial_longitude - $sun_perihelion_ecliptic/ 180;                    // 근점이각
            $sun_diff_celestial_longitude    = 1.919 * sin ($sun_anomaly+ 0.02 * sin (2 * $sun_anomaly);                                        // 황경차
            $sun_true_celestial_longitude    = self::get_degree_between_0_to_360 ($sun_celestial_longitude + $sun_diff_celestial_longitude);        // 진황경
 
            // 달황경
            $moon_celestial_longitude        = 27.836584 + 13.17639648 * $day;                                                                    // 평균 황경
            $moon_perigee                    = 280.425774 + 0.11140356 * $day;                                                                    // 근지점 황경
            $moon_anomaly                    = 3.14159265358979 * ($moon_celestial_longitude - $moon_perihelion_ecliptic/ 180;                    // 근점이각
            $moon_node_celestial_longitude    = 202.489407 - 0.05295377 * $day;                                                                    // 교점황경
            $moon_longitude                    = 3.14159265358979 * ($moon_celestial_longitude - $moon_anomaly/ 180;
            $moon_diff_celestial_longitude    = 5.06889 * sin ($moon_anomaly)
                                            + 0.146111 * sin (2 * $moon_anomaly)
                                            + 0.01 * sin (3 * $moon_anomaly)
                                            - 0.238056 * sin ($sun_anomaly)
                                            - 0.087778 * sin ($moon_anomaly + $sun_anomaly)
                                            + 0.048889 * sin ($moon_anomaly - $sun_anomaly)
                                            - 0.129722 * sin (2 * $moon_longitude)
                                            - 0.011111 * sin (2 * $moon_longitude - $moon_anomaly)
                                            - 0.012778 * sin (2 * $moon_longitude + $moon_anomaly);                                                // 황경차
            $moon_true_celestial_longitude    = self::get_degree_between_0_to_360 ($moon_celestial_longitude + $moon_diff_celestial_longitude);    // 진황경
 
            // 결과
            $celestial_longitude_result        = self::get_degree_between_0_to_360 ($moon_true_celestial_longitude - $sun_true_celestial_longitude);
 
            return $celestial_longitude_result;
        }
 
        // 그레고리력 년/월/일이 들어있는 태음월의 시작합삭일시, 망일시, 끝합삭일시를 계산
        /*
        Array (
            [0] => 2013        // 시작 합삭 년도
            [1] => 7        // 시작 합삭 월
            [2] => 8        // 시작 합삭 일
            [3] => 16        // 시작 합삭 시
            [4] => 15        // 시작 합삭 분
            [5] => 2013        // 망 연도
            [6] => 7        // 망 월
            [7] => 23        // 망 일
            [8] => 2        // 망 시
            [9] => 59        // 망 분
            [10] => 2013    // 끝 합삭 년도
            [11] => 8        // 끝 합삭 월
            [12] => 7        // 끝 합삭 일
            [13] => 6        // 끝 합삭 시
            [14] => 50        // 끝 합삭 분
        )
        */
        // 시작 합삭(Start Conjunction)    = $start_conjunction_year,    $start_conjunction_month,    $start_conjunction_day,    $start_conjunction_hour,    $start_conjunction_min
        // 망월(Full Moon)                = $full_moon_year,            $full_moon_month,            $full_moon_day,            $full_moon_hour,            $full_moon_min
        // 끝 합삭(End Conjunction)        = $end_conjunction_year,    $end_conjunction_month,        $end_conjunction_day,    $end_conjunction_hour,        $end_conjunction_min
        public static function get_conjunction_full_moon ($solar_year$solar_month$solar_day)
        {
            $count_day        = self::count_day_from_start_to_end ($solar_year$solar_month$solar_day19951231);
            $longitude_gap    = self::get_sun_moon_longitude_gap ($count_day);
 
            $j = $count_day;
            $k = $longitude_gap;
 
            while($k > 13.5)
            {
                $j --;
                $k = self::get_sun_moon_longitude_gap ($j);
            };
 
            while($k > 1)
            {
                $j -= 0.04166666666;
                $k = self::get_sun_moon_longitude_gap ($j);
            };
 
            while($j < 359.99)
            {
                $j -= 0.000694444;
                $k = self::get_sun_moon_longitude_gap ($j);
            };
 
            $j += 0.375;
            $j *= 1440;
            $i = (int$j * -1;
            list ($start_year$start_month$start_day$start_hour$start_min= self::get_date_by_target_min ($i1995123100);
 
            $j = $count_day;
            $k = $longitude_gap;
 
            while($k < 346.5)
            {
                $j ++;
                $k = self::get_sun_moon_longitude_gap ($j);
            };
 
            while($k < 359)
            {
                $j += 0.04166666666;
                $k = self::get_sun_moon_longitude_gap ($j);
            };
 
            while($k > 0.01)
            {
                $j += 0.000694444;
                $k = self::get_sun_moon_longitude_gap ($j);
            };
 
            $l    = $j;
            $j += 0.375;
            $j *= 1440;
            $i = (int$j * -1;
            list ($end_conjunction_year$end_conjunction_month$end_conjunction_day$end_conjunction_hour$end_conjunction_min= self::get_date_by_target_min ($i1995123100);
 
            if($solar_month == $end_conjunction_month && $solar_day == $end_conjunction_day)
            {
                $start_conjunction_year        = $end_conjunction_year;
                $start_conjunction_month    = $end_conjunction_month;
                $start_conjunction_day        = $end_conjunction_day;
                $start_conjunction_hour        = $end_conjunction_hour;
                $start_conjunction_min        = $end_conjunction_min;
 
                $j = $l + 26;
 
                $k = self::get_sun_moon_longitude_gap ($j);
                while($k < 346.5)
                {
                    $j ++;
                    $k = self::get_sun_moon_longitude_gap ($j);
                };
 
                while ($k < 359)
                {
                    $j += 0.04166666666;
                    $k = self::moonsundegree ($j);
                };
 
                while ($k > 0.01) {
                    $j += 0.000694444;
                    $k = self::moonsundegree ($j);
                };
 
                $j += 0.375;
                $j *= 1440;
                $i = (int$j * -1;
                list ($end_conjunction_year$end_conjunction_month$end_conjunction_day$end_conjunction_hour$end_conjunction_min= self::get_date_by_target_min ($i1995123100);
            };
 
            $j = self::count_day_from_start_to_end ($start_conjunction_year$start_conjunction_month$start_conjunction_day19951231);
            $j += 12;
 
            $k    = self::get_sun_moon_longitude_gap ($j);
            while($k < 166.5)
            {
                $j ++;
                $k = self::get_sun_moon_longitude_gap ($j);
            };
            
            while($k < 179)
            {
                $j += 0.04166666666;
                $k = self::get_sun_moon_longitude_gap ($j);
            };
 
            while($k < 179.999)
            {
                $j += 0.000694444;
                $k = self::get_sun_moon_longitude_gap ($j);
            };
 
            $j += 0.375;
            $j *= 1440;
            $i = (int$d * -1;
            list ($full_moon_year$full_moon_month$full_moon_day$full_moon_hour$full_moon_min= self::get_date_by_target_min ($i1995123100);
 
            return array (
                $start_conjunction_year$start_conjunction_month$start_conjunction_day$start_conjunction_hour$start_conjunction_min,
                $full_moon_year$full_moon_month$full_moon_day$full_moon_hour$full_moon_min,
                $end_conjunction_year$end_conjunction_month$end_conjunction_day$end_conjunction_hour$end_conjunction_min
            );
        }
 
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
        // 양력 -> 음력 변환
        /*
        Array (
            [0] => 2013    // 음력 연도
            [1] => 6    // 음력 월
            [2] => 9    // 음력 일
            [3] =>        // 음력 윤달 여부(boolean)
            [4] => 1    // 평달(false)/큰달(true) 여부(boolean)
        )
        */
        // 윤달 : 1삭 망월은 29.53059일, 음력에서 달이 지구를 도는 데 354일이 걸린다. 따라서 양력의 기준인 365.23일과 비교해 한 주기마다 11일이 빨라진다.
        // 따라서 음력과 양력간의 차이가 1달이상 벌어지지 않도록 날짜를 밀어주는 것을 윤달이라고 한다.
 
        // 19 양력과 235 삭망월의 날수가 거이 일치하는 '메톤 주기'에 따라 시헌력(중국 청나라의 달력 = 현재의 음력)에 19년간 총 7개의 윤달을 넣으며 2~3년 주기이다.
 
        // 윤년 : 양력에서 자연의 흐음에 대해 생길 수 있는 오차를 보정하기 위해 날이나 주, 달을 인위적으로 삽입하는 해를 말한다. 영어로 Leap year라고 한다.
        // 한국법에서 윤력은 그래고리력에서 여분의 하루인 2월 29일을 추가해 1년동한 날짜의 수를 366일이 되는 해를 말한다.
        // 한국에서는 2월이 29일인 해를 윤년으로 지정하지만 세계력에서는 윤년이 6월 31일이 있는 해로 정한다.
 
        // 윤년의 계산 : 4로 나누어 떨어지지만 100으로도 나누어 떨어지는 해를 윤년으로 그 외에는 평년으로 지정한다.
        // 단 400으로 나누어 떨어지는 해도 윤년으로 지정된다. 예시로 2000년, 2400년이 있다.
        // 보통 4년에 한번 씩 추가되는 하루날은 날수가 가장 적은 2월에 추가된다. 4년마다 2월 29일이 돌아오는 이유이다.
 
        // 대월 / 소월 : 대월은 음력 달의 일수가 31일인 달을 말하고, 소월은 음력 달의 일수가 29일인 달을 말한다.
        public function convert_solar_to_lunar ($solar_year$solar_month$solar_day)
        {
            list (
                $smoyear$smomonth$smoday$smohour$smomin,
                $year_0$month_0$day_0$hour_0$min_0,
                $year_1$month_1$day_1$hour_1$min_1
            ) = self::get_conjunction_full_moon ($solar_year$solar_month$solar_day);
 
            $lunar_day = self::count_day_from_start_to_end ($solar_year$solar_month$solar_day$smoyear$smomonth$smoday+ 1;
 
            $i = abs (self::count_day_from_start_to_end ($smoyear$smomonth$smoday$year_1$month_1$day_1));
            if($i == 30)
                $large_month = 1;    // 대월
            if($i == 29)
                $large_month = 0;    // 소월
 
            list (
                $ingi_name$ingi_year$ingi_month$ingi_day$ingi_hour$ingi_min,
                $mid_name_1$mid_year_1$mid_month_1$mid_day_1$mid_hour_1$mid_min_1,
                $outgi_name$outgi_year$outgi_month$outgi_day$outgi_hour$outgi_min
            ) = self::get_season_by_gregorian ($smoyear$smomonth$smoday$smohour$smomin);
 
            $mid_name_2 = $mid_name_1 + 2;
            if($mid_name_2 > 24)
                $mid_name_2 = 1;
                
            $s0 = self::MONTH[$mid_name_2- self::MONTH[$mid_name_1];
            if($s0 < 0)
                $s0 += 525949;
 
            $s0 *= -1;
 
            list ($mid_year_2$mid_month_2$mid_day_2$mid_hour_2$mid_min_2= self::get_date_by_target_min ($s0$mid_year_1$mid_month_1$mid_day_1$mid_hour_1$mid_min_1);
 
            if(($mid_month_1 == $smomonth && $mid_day_1 >= $smoday|| ($mid_month_1 == $month_1 && $mid_day_1 < $day_1))
            {
                $lunar_month    = ($mid_name_1 - 1/ 2 + 1;
                $leap            = 0;
            }
            else
            {
                if(($mid_month_2 == $month_1 && $mid_day_2 < $day_1|| ($mid_month_2 == $smomonth && $mid_day_2 >= $smoday))
                {
                    $lunar_month    = ($mid_day_2 - 1/ 2 + 1;
                    $leap            = 0;
                }
                else{
                    if($smomonth < $mid_month_2 && $mid_month_2 < $month_1)
                    {
                        $lunar_month    = ($mid_name_2 - 1/ 2 + 1;
                        $leap            = 0;
                    }
                    else
                    {
                        $lunar_month    = ($mid_name_1 - 1/ 2 + 1;
                        $leap            = 1;
                    }
                }
            }
 
            $lunar_year = $smoyear;
            if($lunar_month == 12 && $smomonth == 1)
                $lunar_year --;
 
            if(($lunar_month == 11 && $leap == 1|| $lunar_month == 12 || $lunar_month < 6)
            {
                list ($mid_year_1$mid_month_1$mid_day_1$mid_hour_1$mid_min_1)    = self::get_date_by_target_min (2880$smoyear$smomonth$smoday$smohour$smomin);
                list ($outgi_year$outgi_month$outgi_day$lnp_1$lnp_2)            = self::convert_solar_to_lunar ($mid_year_1$mid_month_1$mid_day_1);
 
                $outgi_day = $lunar_month - 1;
                if($outgi_day == 0)
                    $outgi_day = 12;
                
                if($outgi_day == $outgi_month)
                {
                    if($leap == 1)
                        $leap = 0;
                    else
                    {
                        if($leap == 1)
                        {
                            if($lunar_month != $outgi_month)
                            {
                                $lunar_month --;
                                if($lunar_month == 0)
                                {
                                    $lunar_year        --;
                                    $lunar_month    = 12;
                                };
                                $leap = 0;
                            };
                        }
                        else
                        {
                            if($lunar_month == $outgi_month)
                                $leap = 1;
                            else
                            {
                                $lunar_month --;
                                if($lunar_month == 0)
                                {
                                    $lunar_year        --;
                                    $lunar_month    = 12;
                                }
                            }
                        }
                    }
                }
            }
 
            return array (
                $lunar_year,
                $lunar_month,
                $lunar_day,
                $leap ? true : false,
                $large_month ? true : false
            );
        }
 
        // 음력 -> 양력 변환
        /*
        Array (
            [0] => 2013    // 양력 연도
            [1] => 6    // 양력 월
            [2] => 9    // 양력 일
        )
        */
        public static function convert_lunar_to_solar ($lunar_year_1$lunar_month_1$lunar_day_1$leap = false)
        {
            list (
                $ingi_name$ingi_year$ingi_month$ingi_day$ingi_hour$ingi_min,
                $mid_name$mid_year$mid_month$mid_day$mid_hour$mid_min,
                $outgi_name$outgi_year$outgi_month$outgi_day$outgi_hour$outgi_min
            ) = self::get_season_by_gregorian ($lunar_year_121500);
 
            list (
                $mid_year$mid_month$mid_day$mid_hour$mid_min
            ) = self::get_date_by_target_min ($tmin$ingi_year$ingi_month$ingi_day$ingi_hour$ingi_min);
 
            list (
                $outgi_year$outgi_month$outgi_day$hour$min,
                $yearm$monthm$daym$hourm$minm,
                $year_1$month_1$day_1$hour_1$min_1
            ) = self::get_conjunction_full_moon ($mid_year$mid_month$mid_day);
            
            list (
                $lunar_year_2$lunar_month_2$lunar_day_2$lnp_1$lnp_2
            ) = self::convert_solar_to_lunar ($outgi_year$outgi_month$outgi_day);
 
            if($lunar_year_1 == $lunar_year_2 && $lunar_month_1 == $lunar_month_2)
            {
                $tmin = -1440 * $lunar_day_1 + 10;
                list (
                    $solar_year$solar_month$solar_day$hour$min
                ) = self::get_date_by_target_min ($tmin$year_1$month_1$day_100);
 
                if($leap)
                {
                    list (
                        $lunar_year_2$lunar_month_2$lunar_day_2$lnp_1$lnp_2
                    ) = self::convert_solar_to_lunar ($year_1$month_1$day_1);
                    if$lunar_year_1 == $lunar_year_2 && $lunar_month_1 == $lunar_month_2)
                    {
                        $tmin = -1440 * $lunar_day_1 + 10;
                        list (
                            $solar_year$solar_month$solar_day$hour$min
                        ) = self::get_date_by_target_min ($tmin$year_1$month_1$day_100);
                    }
                }
            }
            else
            {
                list (
                    $lunar_year_2$lunar_month_2$lunar_day_2$lnp_1$lnp_2
                ) = self::convert_solar_to_lunar ($year_1$month_1$day_1);
                if($lunar_year_1 == $lunar_year_2 && $lunar_month_1 == $lunar_month_2)
                {
                    $tmin = -1440 * $lunar_day_1 + 10;
                    list (
                        $solar_year$solar_month$solar_day$hour$min
                    ) = self::get_date_by_target_min ($tmin$year_1$month_1$day_100);
                }
            }
 
            return array (
                $solar_year,
                $solar_month,
                $solar_day
            );
        }
 
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
        // 그레고리력 날짜를 요일의 배열 번호로 변환
        public static function get_week_day_by_gregorian ($solar_year$solar_month$solar_day)
        {
            $d = self::count_day_from_start_to_end (
                $solar_year$solar_month$solar_day,
                self::UNIT_YEAR, self::UNIT_MONTH, self::UNIT_DAY
            );
 
            $i = self::get_integer_share ($d7);
            $d -= $i * 7;
 
            while($d > 6 || $d < 0)
            {
                if($d > 6)
                    $d -= 7;
                else
                    $d += 7;
            }
 
            if($d < 0)
                $d += 7;
 
            return $d;
        }
 
        // 그레고리력의 날짜에 대한 28수를 계산
        public static function get_lunar_mansions_by_gregorian ($solar_year$solar_month$solar_day)
        {
            $d = self::count_day_from_start_to_end (
                $solar_year$solar_month$solar_day,
                self::UNIT_YEAR, self::UNIT_MONTH, self::UNIT_DAY
            );
 
            $i = self::get_integer_share ($d28);
            $d -= $i * 28;
 
            while($d > 27 || $d < 0)
            {
                if($d > 27)
                    $d -= 28;
                else
                    $d += 28;
            }
 
            if($d < 0)
                $d += 7;
 
            $d -= 11;
 
            if($d < 0)
                $d += 28;
 
            return $d;
        }
    }
?>
cs

lunar_solar.php

더보기
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
<?
    require_once("lunar_solar_base.php");
    
    class LunarSolar extends LunarSolarBase
    {
        // 그레고리안력은 1년을 365.2425일로 정하는 윤년을 포함하는 양력을 말한다. 또한 세계 표준으로 사용하는 역법이다. 기본적으로 율리우스력을 그대로 따르지만 윤년을 정하는 규칙을 두가지 추가했다.
        // 1. 끝자리가 00으로 끝나는 해는 평년이다.
        // 2. 그중 400으로 나누어 떨어지는 해는 윤년이다.
        // 기존 율리우스력은 400년 동안 윤년이 약 100회지만, 그레고리안력은 97회로 줄였다.
 
        // 율리우스력은 그레고리안력의 기초가 되는 양력의 기준이다. 기본 구조는 1년 365일에 4년마다 한번씩 윤년(하루를 더해 366일을 1년으로 한다.)
        // 이것으로 4년마다 한번씩 2월 29일이 생기는 이유이다.
 
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
        // YYYY-MM-DD 형식의 날짜를 반환
        // $date = array ($year, $month, $day)
        public static function get_hyphen_date ($date)
        {
            list ($year$month$day= $date;
 
            return sprintf (
                '%d-%s%d-%s%d',
                $year,
                ($month < 10) ? '0' : '',
                (int$month,
                ($day < 10) ? '0' : '',
                (int$day
            );
        }
 
        // AD/BC 타입의 연도를 반환
        /*
            input    :
                LunarSolar::get_readable_year (-2333);
            output    :
                BC 2333
        */
        public static function get_readable_year ($year)
        {
            if($year < 1)
            {
                $year = ($year * -1+ 1;
                $type = 'BC';
            }
            else
                $type = 'AD';
 
            return sprintf ('%s %d'$type$year);
        }
 
        // YYYY-MM-DD 또는 array ((string) YYYY, (string) MM, (string) DD 입력값을 array ((int) $year, (int) $month, (int) $day))로 변환
        // $date = YYYY-MM-DD
        // $date = array ((string) YYYY, (string) MM, (stirng) DD)
        public static function get_split_date ($date)
        {
            if(is_array ($date))
                $date = self::get_hyphen_date ($date);
 
            $minus = ($date[0== '-') ? true : false;
            $date = $minus ? substr ($date1) : $date;
 
            $result = preg_split ('/-/'$date);
            if($minus)
                $result[0*= -1;
            
            foreach($result AS $key => $value)
            {
                $result[$key= (int$value;
            }
            
            return $result;
        }
 
        // 입력된 날짜 형식을 연/월/일의 배열로 반환
        /*
            input    : 
                LunarSolar::convert_to_args (
                    2013-07-13    or
                    2013-7-13    or
                    20130713    or
                    1373641200    or
                    NULL
                );
            output    :
                Array
                (
                    [0]    => 2013,
                    [1]    => 7,
                    [2]    => 13,
                );
        */
        public static function convert_to_args (&$date$lunar_date = false)
        {
            if($date == null)
            {
                $year    = (int) date ('Y');
                $month    = (int) date ('m');
                $day    = (int) date ('d');
            }
            else
            {
                if(in_numeric ($date&& $date > 30000000)
                {
                    $year    = (int) date ('Y'$date);
                    $month    = (int) date ('m'$date);
                    $day    = (int) date ('d'$date);
                }
                else
                {
                    if(pref_match ('/^(-?[0-9]{1,4})[\/-]?([0-9]{1,2})[\/-]?([0-9]{1,2})$/', trim ($date), $match))
                    {
                        array_shift ($match);
                        list ($year$month$day= $match;
                    }
                    else
                    {
                        throw new Exception('Invalid Date Format');
                        return false;
                    }
                }
 
                // 날짜가 음력일 경우 아래가 실행이 되면 측정되는 날짜가 달라질 수 있음
                if(!$lunar && $year > 1969 && $year < 2038)
                {
                    $fixed_date = mktime (000$month$day$year);
                    $year    = (int) date ('Y'$fixed_date);
                    $month    = (int) date ('m'$fixed_date);
                    $day    = (int) date ('d'$fixed_date);
                }
                else
                {
                    if($month > 12 || $day > 31)
                    {
                        throw new Exception('Invalid Date Format');
                        return false;
                    }
                }
            }
            $date = self::get_hyphen_date (array ($year$month$day));
 
            return array ($year$month$day);
        }
 
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
        // 윤년인지 체크
        /*
            input    :
                LunarSolar::is_leap(1992);
            output    :
                true
        */
        // 1582년 이전은 율리우스 달력으로 판단, 또한 false도 율리우스력으로 간주해 판단
        public function is_leap ($year$julian = false)
        {
            if($julian || $year < 1583)
                return ($year % 4) ? false : true;
 
            if(($year % 400== 0)
                return true;
            
            if(($year % 4== 0 && ($year % 100!= 0)
                return true;
            
            return false;
        }
 
        // 해당 날짜가 그레고리안 범위인지 체크
        public function is_gregorian ($year$month$day = 1)
        {
            if((int$month < 10)
                $month = '0'.(int$month;
            if((int$day < 10)
                $day = '0'.(int$day;
            
            $check = $year.$month.$day;
 
            if($check < 15821015)
                return false;
            
            return true;
        }
 
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
        // 그레고리안력을 율리우스력으로 변환
        /*
        stdClass Object
        (
            [julian(fmt)]    => 2013-06-09    // YYYY-MM-DD 형식의 율리우스 날짜
            [year]            => 2013            // 연도
            [month]            => 6            // 월
            [day]            => 9            // 일
            [week]            => 화            // 요일
        );
        */
        // $date = 그레고리안 연/월/일 배열 또는 Julian date count
        public static function convert_gregorian_to_julian ($date)
        {
            if(is_array ($date))
            {
                $hyphen_date = self::get_hyphen_date ($date);
                list ($year$month$day= self::get_split_date($hyphen_date);
 
                $date = self::convert_gregorian_date_to_julian_date(array ($year$month$day));
            }
 
            if(extension_loaded ('calendar'))
            {
                $result_date = (object) cal_from_jd ($date, CAL_JULIAN);
                if($result_date -> year < 0)
                    $result_date -> year ++;
                
                return (objectarray (
                    'fmt'    => self::get_hyphen_date(
                                                    array (
                                                        $result_date -> year,
                                                        $result_date -> month,
                                                        $result_date -> day
                                                        )
                                                ),
                    'year'    => $result_date -> year,
                    'month'    => $result_date -> month,
                    'day'    => $result_date -> day,
                    'week'    => $result_date -> dow
                );
            }
 
            if(is_float ($date))
                list ($X$Y= preg_split ('/\./'$date);
            else
            {
                $X = $date;
                $Y = 0;
            }
 
            if($date < 2299161)
                $A = $X;
            else
            {
                $alpha = (int) ($X - 1867216.25 / 36524.25);
                $A = $X + 1 + $alpha - (int) ($alpha / 4);
            }
 
            $B = $A + 1524;
            $C = (int) (($B - 122.1/ 365.25);
            $D = (int) (365.25 * $C);
            $E = (int) (($B - $D/ 30.6001);
 
            $day    = $B - $D - (int) (30.6001 *$E+ $F;
            $month    = ($E < 14) ? $E - 1 : $E - 13;
            $year    = $C - 4715;
            if($month > 2)
                $year --;
            
            $week = ($date + 1.5) % 7;
 
            return (objectarray (
                'fmt'    => self::get_hyphen_date (array ($year$month$day)),
                'year'    => $year,
                'month'    => $month,
                'day'    => $day,
                'week'    => $week
            );
        }
 
        public static function get_modulus ($value_1$value_2)
        {
            return ($value_1 % $value_2 + $value_2) % $value_2;
        }
 
        // 율리우스력을 그레고리안력으로 변환
        /*
        stdClass Object
        (
            [gregorian(fmt)]    => 2013-06-09    // YYYY-MM-DD 형식의 Julian 날짜
            [year]                => 2013            // 연도
            [month]                => 6            // 월
            [day]                => 9            // 일
            [week]                => 화            // 요일
        );
        */
        // $julian_date = 율리우스 연/월/일 배열 또는 Julian date count
        public static function convert_julian_to_gregorian ($julian_date$pure = false)
        {
            if(is_array ($julian_date))
            {
                list ($year$month$day= self::get_split_date ($julian_date);
                $julian_date = self::calculate_to_julian_date (array ($year$month$day), true);
            }
 
            if(extension_loaded ('calendar'&& $pure == false)
            {
                $result_date = (object) cal_from_jd ($julian_date, CAL_GREGORIAN);
                if($result_date -> year < 0)
                    $result_date -> year ++;
                
                return (objectarray (
                    'fmt'    => self::get_hyphen_date (
                                                    array (
                                                        $result_date -> year,
                                                        $result_date -> month,
                                                        $result_date -> day
                                                        )
                                                ),
                    'year'    => $result_date -> year,
                    'month'    => $result_date -> month,
                    'day'    => $result_date -> day,
                    'week'    => $result_date -> dow,
                );
            }
            /* 01-01-02 부터 이전은 맞지 않음 */
            // $a = (int) $julian_date + 1401;
            // $a = (int) ($a + (((4 * $julian_date + 274277) / 146097) * 3) / 4 - 38);
            // $b = 4 * $a + 3;
            // $c = (int) (($b % 1461) / 4);
            // $d = 5 * $c + 2;
            // $day        = (int) (($d % 153) / 5 + 1);
            // $month    = (int) ((($d / 153 + 2) % 12) + 1);
            // $year    = (int) ($b / 1461 - 4716 + (12 + 2 - $month) / 12);
 
            $re_julian_date = floor ($julian_date - 0.5+ 0.5;
            // GREGORIAN_EPOCH 1721425.5
            $depoch            = $re_julian_date - 1721425.5;
            $quadricent        = floor ($depoch / 146097);
            $dqc            = self::get_modulus ($depoch146097);
            $cent            = floor ($dqc / 36524);
            $decent            = self::get_modulus ($dqc36524);
            $quad            = floor ($decent / 1461);
            $dquad            = self::get_modulus ($dcent1461);
            $index            = floor ($dquad / 365);
 
            $year = ($quadricent * 400+ ($cent * 100+ ($quad * 4+ $index;
            if(!($cent == 4 || $index == 4))
                $year ++;
 
            $year_day    = $re_julian_date - self::calculate_to_julian_date (array ($year11));
            $leap        = $re_julian_date < self::calculate_to_julian_date (array ($year31)) ? 0 : self::is_leap ($year) ? 1 : 2;
            $month        = floor (((($year_day + $leap* 12+ 373/ 367);
            $day        = ceil ($re_julian_date - self::calculate_to_julian_date (array ($year$month1))) + 1;
            
            $week        = ($julian_date + 1.5) % 7;
 
            return (objectarray (
                'fmt'    => self::get_hyphen_date (array ($year$month$day)),
                'year'    => $year,
                'month'    => $month,
                'day'    => $day,
                'week'    => $week
            );
        }
 
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
        // 그레고리안 날짜를 Julian date로 변환 (by PURE PHP CODE)
        /*
        1. Y = 해당년도, M = 월(1월 = 1, 2월 = 2), D = 해당 월의 날짜.
            D는 시간값도 포함한 소수값을 생각해야한다. 예시로 3일 12시 UT라면 D = 3.5 이다.
        2. M > 2인 경우 year, M 변경하지 않는다. M = 1 or 2인경우 Y = Y - 1, M = M + 12로 계산한다.
        3. 그레고리안력의 경우는 아래와 같이 계산한다.
            A = INT(Y / 100), B = 2 - A + INT(A / 4)
            여기서 INT는 ()안에 들어간 값을 넘지 않는 가장 큰 정수이다.
        4. Julian date의 계산은 아래와 같다.
            JULIAN_DATE = INT(365.25 (Y + 4716)) + INT(30.6001 (M + 1)) + D + B - 1524.5
            여기서 30.6001은 30.6을 써야한다. 하지만 컴퓨터 계산기 10.6이여 하는데 10.5999...로 표현되는 경우가 발생시에는
            INT(10.6)과 INT(10.5999...)의 결과가 달라진다. 따라서 이 문제에 대처하기 위해 30.6001을 사용한 것이다.
        */
        // $date = array($year, $month, $day)
        public static function calculate_to_julian_date_pure ($date$julian = false)
        {
            list ($year$month$day= $date;
 
            if($month <= 2)
            {
                $year --;
                $month += 12;
            }
 
            $A = (int) ($year / 100);
            $B = $julian ? 0 : 2 - $A + (int) ($A / 4);
            $C = (int) (365.25 * ($year + 4716));
            $D = (int) (30.6001 * ($month + 1));
 
            return ceil ($C + $D + $day + $B - 1524.5);
        }
 
        // 그레고리안 날짜를 Julian date로 변환 (by Calendar Extendsion)
        // $date = array ($year, $month, $day)
        public static function calculate_to_julian_date_ext ($date$julian = false)
        {
            list ($year$month$day= $date;
 
            $timezone = date_default_timezone_get ();
            date_default_timezone_set ('UTC');
 
            $correct_julian = $julian ? 'JulianToJulianDate' : 'GregorianToJulianDate';
            if($year < 1)
                $year --;
            
            $julian_result = $correct_julian ((int$month, (int$day, (int$year);
 
            date_default_timezone_set ($timezone);
            return $julian_result;
        }
 
        // 그레고리안 날짜를 Julian date로 변환
        // $date = array ($year, $month, $day)
        public static function calculate_to_julian_date ($date$julian = false)
        {
            if(extension_loaded ('calendar'))
                return self::calculate_to_julian_date_ext ($date$julian);
 
            return self::calculate_to_julian_date_pure ($date$julian);
        }
 
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
        // Localtime을 UTC로 변환
        // $date = dateformat (YYYY-MM-DD HH:II:SS)
        public static function convert_local_time_to_UTC ($date)
        {
            $time            = strtotime ($date);
            $timezone        = date_default_timezone_get ();
            date_default_timezone_set ('UTC');
 
            $time_result    = date ('Y-m-d-H-i-s'$time);
            date_default_timezone_set ($timezone);
 
            return $time_result;
        }
 
        // 합삭/망 절기 시간을 UTC로 변환 후, Julian date로 반환
        // $date = dateformat (YYYY-MM-DD HH:II:SS)
        public static function convert_date_to_utc_julian ($date)
        {
            $utc_date = self::convert_local_time_to_UTC ($date);
            list ($year$month$day$hour$min$sec= self::get_split_date ($utc_date);
 
            $check            = $year.$month.$day;
            $julian            = ($check < 18451015) ? true : false;
            $julian_result    = self::calculate_to_julian_date (array ($year$month$day), $julian);
 
            if(($hour - 12< 0)
            {
                $hour        = 11 - $hour;
                $min        = 60 - $min;
                $utc_date    = (($hour * 3600 + $min * 60/ 86400* -1;
            }
            else
                $utc_date    = (($hour - 12* 3600 + $min * 60/ 86400;
 
            return $julian_result + $utc_date;
        }
 
        // 1582년 10월 15일 이전의 date를 Julian date로 변환
        public static function fix_calendar ($year$month$day)
        {
            if($month < 10)
                $month = '0'.$month;
            if($day < 10)
                $day = '0'.$day;
 
            // 15821005 ~ 15821014 까지는 그레고리안 달력에서 존재하지 않는다.
            // 따라서 이 기간의 날짜는 율리우스 달력과 같은 날짜로 변경한다. (10씩 빼준다.)
            $check = $year.$month.$day;
            if($check > 15821004 && $check < 15821015)
            {
                $julian            = self::calculate_to_julian_date (array ((int$year, (int$month, (int$day));
                $julian            -= 10;
                $julian_result    = self::convert_julian_to_gregorian ($julian);
                list ($year$month$day= array (
                    $julian_result -> year,
                    $julian_result -> month,
                    $julian_result -> day
                );
            }
 
            // 15821005 보다 과거의 날짜는 그레고리안 달력이 없기 때문에 율리우스 달력으로 표현한다.
            if(self::is_gregorian ((int$year, (int$month, (int$day=== false)
            {
                $julian_result = self::convert_julian_to_gregorian (array ((int$year, (int$month, (int$day));
                list ($year$month$day= array (
                    $julian_result -> year,
                    $julian_result -> month,
                    $julian_result -> day
                );
            }
 
            return array ($year$month$day);
        }
 
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
        // 양력 -> 음력 변환
        /*
            input    :
                LunarSolar::convert_to_lunar (
                    2013-07-16    or
                    2013-7-16    or
                    20130716    or
                    1373900400    or
                    NULL
                );
            
            output    :
                stdClass Object
                (
                    [fmt]            => 2013-06-09    // YYYY-MM-DD 형식의 음력 날짜
                    [dangi]            => 4346            // 단기(단군기원, 檀君紀元) 서기 2020년 = 단기 4353년
                    [hyear]            => AD 2013        // AD/BC 형식의 연도
                    [year]            => 2013            // 연도
                    [month]            => 6            // 월
                    [day]            => 9            // 일
                    [leap]            => (boolean)    // 음력 윤달 여부
                    [large_month]    => 1            // 평달(소월, 小月) / 큰달(대월, 大月) 여부
                    [kor_week]        => 화            // 요일
                    [han_week]        => 火            // 한자 요일
                    [unixstamp]        => 1373900400    // unixstamp (양력)
                    [kor_ganji]        => 계사            // 세차(년)
                    [han_ganji]        => 癸巳            // 한자 세차(년)
                    [kor_gan]        => 계            // 10간
                    [han_gan]        => 癸            // 한자 십간
                    [kor_ji]        => 사            // 십이지
                    [han_ji]        => 巳            // 한자 십이지
                    [zodiac]        => 뱀            // 띠
                );
        */
        // $date의 형식(int 또는 string)
        // 1. unixstmap (1970년 12월 15일 이후부터만 가능)
        // 2. Ymd or Y-m-d
        // 3. null date (현재 시간)
        // 4. 1582년 10월 15일 이전의 날짜는 율리우스력의 날짜로 취급
        public static function convert_to_lunar ($date = null)
        {
 
            list ($year$month$day= self::convert_to_args ($date);
            list ($year$month$day= self::fix_calendar ($year$month$day);
 
            $lunar_result = LunarSolarBase::convert_solar_to_lunar ($year$month$day);
            list ($year$month$day$leap$large_month= $lunar_result;
 
            $week = LunarSolarBase::get_week_day_by_gregorian ($year$month$day);
 
            $count_value_1 = ($year + 6) % 10;
            $count_value_2 = ($year + 8) % 12;
 
            if($count_value_1 < 0)
                $count_value_1 += 10;
            if($count_value_2 < 0)
                $count_value_2 += 12;
 
            return (objectarray (
                'fmt'            => self::get_hyphen_date ($lunar_result),
                'dangi'            => $year + 2333,
                'hyear'            => self::get_readable_year ($year),
                'year'            => $year,
                'month'            => $month,
                'day'            => $day,
                'leap'            => $leap,
                'large_month'    => $large_month,
                'kor_week'        => LunarSolarBase::KOR_WEEK[$week],
                'han_week'        => LunarSolarBase::HAN_WEEK[$week],
                'unixstamp'        => mktime (000$month$day$year),
                'kor_ganji'        => LunarSolarBase::KOR_GAN[$count_value_1].LunarSolarBase::KOR_JI[$count_value_2],
                'han_ganji'        => LunarSolarBase::HAN_GAN[$count_value_1].LunarSolarBase::HAN_JI[$count_value_2],
                'kor_gan'        => LunarSolarBase::KOR_GAN[$count_value_1],
                'han_gan'        => LunarSolarBase::HAN_GAN[$count_value_1],
                'kor_ji'        => LunarSolarBase::KOR_JI[$count_value_2],
                'han_ji'        => LunarSolarBase::HAN_JI[$count_value_2],
                'zodiac'        => LunarSolarBase::ZODIAC[$count_value_2]
            );
        }
 
        // 음력 -> 양력 변환
        /*
            input    :
                LunarSolar::convert_to_solar (
                    2013-06-09    or
                    2013-6-9    or
                    20130609    or
                    NULL
                );
            output    :
                stdClass Object
                (
                    [jd]        => 2456527        // Julian Date Count
                    [fmt]        => 2013-07-16    // YYYY-MM-DD 형식의 날짜 (15821015 이전은 율리우스력)
                    [gregory]    => 2013-07-16    // 그레고리안 달력
                    [julian]    => 2013-08-09    // 율리우스 달력
                    [dangi]        => 4346            // 단기(단군기원, 檀君紀元) 서기 2020년 = 단기 4353년
                    [hyear]        => AD 2013        // AD/BC 형식의 연도
                    [year]        => 2013            // 연도
                    [month]        => 7            // 월
                    [day]        => 16            // 일
                    [kor_week]    => 화            // 요일
                    [han_week]    => 火            // 한자 요일
                    [unixstamp]    => 1373900400    // unixstamp (양력)
                    [kor_ganji]    => 계사            // 세차(년)
                    [han_ganji]    => 癸巳            // 한자 세차(년)
                    [kor_gan]    => 계            // 10간
                    [han_gan]    => 癸            // 한자 십간
                    [kor_ji]    => 사            // 십이지
                    [han_ji]    => 巳            // 한자 십이지
                    [zodiac]    => 뱀            // 띠
                );
            만약 구하려는 음력월의 윤달 여부를 모른다면 아래와 같은 확인 과정이 필요하다.
                $lunar        = '2013-06-09';
                $solar_date    = LunarSolar::convert_to_solar ($lunar);
                $lunar_date    = LunarSolar::convert_to_lunar ($solar -> fmt);
                if($lunar != $lunar_date -> fmt)
                    $solar_date = LunarSolar::convert_to_solar ($lunar, true);
        */
        // $date의 형식(int 또는 string)
        // 1. unixstmap (1970년 12월 15일 이후부터만 가능)
        // 2. Ymd or Y-m-d
        // 3. null date (현재 시간)
        // $leap = boolean (윤달 여부)
        public static function convert_to_solar ($date = null$leap = false)
        {
            list ($year$month$day= self::convert_to_args ($datetrue);
 
            $solar_result = LunarSolarBase::convert_lunar_to_solar ($year$month$day$leap);
            list ($year$month$day= $solar_result;
 
            $week = LunarSolarBase::get_week_day_by_gregorian ($year$month$day);
            
            $julian_date    = self::calculate_to_julian_date ($solar_result);
            $julian_result    = self::convert_gregorian_to_julian ($julian_date);
            $julian_fmt        = $julian -> fmt;
            $gregorian_fmt    = self::get_hyphen_date ($solar_result);
            $fmt            = ($julian_date < 2299161) ? $julian_fmt : $gregorian_fmt;
 
            $count_value_1 = ($year + 6) % 10;
            $count_value_2 = ($year + 8) % 12;
 
            if($count_value_1 < 0)
                $count_value_1 += 10;
            if($count_value_2 < 0)
                $count_value_2 += 12;
 
            return (objectarray (
                'jd'        => $julian_date,
                'fmt'        => $fmt,
                'gregory'    => $gregorian_fmt,
                'julian'    => $julian_fmt,
                'dangi'        => $year + 2333,
                'hyear'        => self::get_readable_year ($year),
                'year'        => $year,
                'month'        => $month,
                'day'        => $day,
                'kor_week'    => LunarSolarBase::KOR_WEEK[$week],
                'han_week'    => LunarSolarBase::HAN_WEEK[$week],
                'unixstamp'    => mktime (000$month$day$year),
                'kor_ganji'    => LunarSolarBase::KOR_GAN[$count_value_1].LunarSolarBase::KOR_JI[$count_value_2],
                'han_ganji'    => LunarSolarBase::HAN_GAN[$count_value_1].LunarSolarBase::HAN_JI[$count_value_2],
                'kor_gan'    => LunarSolarBase::KOR_GAN[$count_value_1],
                'han_gan'    => LunarSolarBase::HAN_GAN[$count_value_1],
                'kor_ji'    => LunarSolarBase::KOR_JI[$count_value_2],
                'han_ji'    => LunarSolarBase::HAN_JI[$count_value_2],
                'zodiac'    => LunarSolarBase::ZODIAC[$count_value_2]
            );
        }
 
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
        // 세차(년), 월건(월), 일진(일) 데이터를 얻는다.
        /*
            input    :
                LunarSolar::get_day_fortune (
                    '2013-07-16'    or
                    '2013-7-16'        or
                    '20130716'        or
                    '1373900400'    or
                    NULL
                );
            
            output    :
                stdClass Object
                (
                    [data]        => stdClass Object
                        (
                            [year]    => 29    // 세차 index
                            [month]    => 55    // 월건 index
                            [day]    => 19    // 일진 index
                        )
                    [kor_year]    => 계사    // 세차(년) 값
                    [kor_month]    => 기미    // 월건(월) 값
                    [kor_day]    => 계미    // 일진(일) 값
                    [han_year]    => 癸巳    // 한자 세차(년) 값
                    [han_month]    => 己未    // 한자 월건(월) 값
                    [han_day]    => 癸未    // 한자 일진(일) 값
                );
        */
        // $date의 형식
        // 1. unixstmap (1970년 12월 15일 이후부터만 가능)
        // 2. Ymd or Y-m-d
        // 3. null date (현재 시간)
        // 4. 1582년 10월 15일 이전의 날짜는 율리우스력의 날짜로 취급
        public static function get_day_fortune ($date = null)
        {
            list ($year$month$day= self::convert_to_args ($date);
            list ($year$month$day= self::fix_calendar ($year$month$day);
 
            list ($sexagenary$year$month$day$hour= LunarSolarBase::get_sexagenary_by_gregorian ($year$month$day10);
 
            return (objectarray (
                'data'        => (objectarray (
                                    'year'    => $year,
                                    'month'    => $month,
                                    'day'    => $day
                                ),
                'kor_year'    => LunarSolarBase::KOR_GANJI[$year],
                'kor_month'    => LunarSolarBase::KOR_GANJI[$month],
                'kor_day'    => LunarSolarBase::KOR_GANJI[$day],
                'han_year'    => LunarSolarBase::HAN_GANJI[$year],
                'han_month'    => LunarSolarBase::HAN_GANJI[$month],
                'han_day'    => LunarSolarBase::HAN_GANJI[$day]
            );
        }
 
        // 특정일의 28수를 구한다.
        /*
            input    :
                LunarSolar::get_28_day (
                    '2013-07-16'    or
                    '2013-7-16'        or
                    '20130716'        or
                    '1373900400'    or
                    NULL
                );
            
            output    :
                stdClass Object
                (
                    [data]            => 5    // 28일 데이터 index
                    [kor_28_days]    => 미    // 28일 데이터 한글
                    [han_28_days]    => 尾    // 28일 데이터 한자
                );
        */
        // $date의 형식
        // 1. unixstmap (1970년 12월 15일 이후부터만 가능)
        // 2. Ymd or Y-m-d
        // 3. null date (현재 시간)
        // 4. 1582년 10월 15일 이전의 날짜는 율리우스력의 날짜로 취급
        public static function get_28_day ($date = null)
        {
            if(is_object ($date))
            {
                $result = $date -> data + 1;
                if($result >= 28)
                    $result %= 28;
 
                goto skip_work;
            }
 
            list ($year$month$day= self::convert_to_args ($date);
            list ($year$month$day= self::fix_calendar ($year$month$day);
            $result = LunarSolarBase::get_lunar_mansions_by_gregorian ($year$month$day);
 
            skip_work:
 
            return (objectarray (
                'data'            => $result,
                'kor_28_days'    => LunarSolarBase::KOR_28_DAYS[$result],
                'han_28_days'    => LunarSolarBase::HAN_28_DAYS[$result]
            );
        }
 
        // 금월(이번달) 초입/중기와 익월(다음달) 초입 데이터 반환
        /*
            input    :
                LunarSolar::get_seasonal_date (
                    '2013-07-16'    or
                    '2013-7-16'        or
                    '20130716'        or
                    '1373900400'    or
                    NULL
                );
            
            output    :
                stdClass Object
                (
                    [this_month_entry]    => stdClass Object
                    (
                        [kor_name]    => 소서
                        [han_name]    => 小暑
                        [hyear]        => AD 2013
                        [year]        => 2013
                        [month]        => 7
                        [day]        => 7
                        [hour]        => 7
                        [min]        => 49
                    )
                    [this_month_middle]    => stdClass Object
                    (
                        [kor_name]    => 대서
                        [han_name]    => 大暑
                        [hyear]        => AD 2013
                        [year]        => 2013
                        [month]        => 7
                        [day]        => 23
                        [hour]        => 1
                        [min]        => 11
                    )
                    [next_month_entry]    => stdClass Object
                    (
                        [kor_name]    => 입추
                        [han_name]    => 立秋
                        [hyear]        => AD 2013
                        [year]        => 2013
                        [month]        => 8
                        [day]        => 7
                        [hour]        => 17
                        [min]        => 36
                    )
                );
        */
        // $date의 형식
        // 1. unixstmap (1970년 12월 15일 이후부터만 가능)
        // 2. Ymd or Y-m-d
        // 3. null date (현재 시간)
        // 4. 1582년 10월 15일 이전의 날짜는 율리우스력의 날짜로 취급
        public static function get_seasonal_date ($date = null)
        {
            list ($year$month$day= self::convert_to_args ($date);
            list ($year$month$day= self::fix_calendar ($year$month$day);
 
            list (
                $ingi_name$ingi_year$ingi_month$ingi_day$ingi_hour$ingi_min,
                $mid_name$mid_year$mid_month$mid_day$mid_hour$mid_min,
                $outgi_name$outgi_year$outgi_month$outgi_day$outgi_hour$outgi_min
            ) = LunarSolarBase::get_season_by_gregorian ($year$month2010);
            
            // 금월 초입
            $julian_this_month_entry = self::convert_date_to_utc_julian (
                sprintf (
                    '%s %s:%s:00',
                    self::get_hyphen_date (array ($ingi_year$ingi_month$ingi_day)),
                    $ingi_hour < 10 ? '0'.$ingi_hour : $ingi_hour,
                    $ingi_min < 10 ? '0'.$ingi_min : $ingi_min
                )
            );
 
            // 1852-10-15 이전이면 julian으로 변경
            if(self::is_gregorian ($ingi_year$ingi_month$ingi_day=== false)
            {
                $result        = self::convert_gregorian_to_julian (array ($ingi_year$ingi_month$ingi_day));
                $ingi_year    = $result -> year;
                $ingi_month    = $result -> month;
                $ingi_day    = $result -> day;
            }
 
            // 금월 중기
            $julian_this_month_middle = self::convert_date_to_utc_julian (
                sprintf (
                    '%s %s:%s:00',
                    self::get_hyphen_date (array ($mid_year$mid_month$mid_day)),
                    $mid_hour < 10 ? '0'.$mid_hour : $mid_hour,
                    $mid_min < 10 ? '0'.$mid_min : $mid_min
                )
            );
 
            // 1852-10-15 이전이면 julian으로 변경
            if(self::is_gregorian ($ingi_year$ingi_month$ingi_day=== false)
            {
                $result        = self::convert_gregorian_to_julian (array ($ingi_year$ingi_month$ingi_day));
                $ingi_year    = $result -> year;
                $ingi_month    = $result -> month;
                $ingi_day    = $result -> day;
            }
 
            // 익월 초입
            $julian_next_month_entry = self::convert_date_to_utc_julian (
                sprintf (
                    '%s %s:%s:00',
                    self::get_hyphen_date (array ($outgi_year$outgi_month$outgi_day)),
                    $outgi_hour < 10 ? '0'.$outgi_hour : $outgi_hour,
                    $outgi_min < 10 ? '0'.$outgi_min : $outgi_min
                )
            );
 
            // 1852-10-15 이전이면 julian으로 변경
            if(self::is_gregorian ($outgi_year$outgi_month$outgi_day=== false)
            {
                $result            = self::convert_gregorian_to_julian (array ($outgi_year$outgi_month$outgi_day));
                $outgi_year        = $result -> year;
                $outgi_month    = $result -> month;
                $outgi_day        = $result -> day;
            }
 
            return (objectarray (
                'this_month_entry'  => (objectarray (
                    'kor_name'    => LunarSolarBase::KOR_MONTH_STR[$ingi_name],
                    'han_name'    => LunarSolarBase::HAN_MONTH_STR[$ingi_name],
                    'hyear'        => self::get_readable_year ($ingi_year),
                    'year'        => $ingi_year,
                    'month'        => $ingi_month,
                    'day'        => $ingi_day,
                    'hour'        => $ingi_hour,
                    'min'        => $ingi_min,
                    'julian'    => $julian_this_month_entry
                ),
                'this_month_middle' => (objectarray (
                    'kor_name'    => LunarSolarBase::KOR_MONTH_STR[$mid_name],
                    'han_name'    => LunarSolarBase::HAN_MONTH_STR[$mid_name],
                    'hyear'        => self::get_readable_year ($mid_year),
                    'year'        => $mid_year,
                    'month'        => $mid_month,
                    'day'        => $mid_day,
                    'hour'        => $mid_hour,
                    'min'        => $mid_min,
                    'julian'    => $julian_this_month_middle
                ),
                'next_month_entry'  => (objectarray (
                    'kor_name'    => LunarSolarBase::KOR_MONTH_STR[$outgi_name],
                    'han_name'    => LunarSolarBase::HAN_MONTH_STR[$outgi_name],
                    'hyear'        => self::get_readable_year ($outgi_year),
                    'year'        => $outgi_year,
                    'month'        => $outgi_month,
                    'day'        => $outgi_day,
                    'hour'        => $outgi_hour,
                    'min'        => $outgi_min,
                    'julian'    => $julian_next_month_entry
                )
            );
        }
 
        // 양력일에 대한 음력월의 합삭/망 데이터 반환
        /*
            input    :
                LunarSolar::get_moon_status (
                    '2013-07-16'    or
                    '2013-7-16'        or
                    '20130716'        or
                    '1373900400'    or
                    NULL
                );
            
            output    :
                stdClass Object
                (
                    [new_moon]    => stdClass Object
                    (
                        [hyear]        => AD 2013
                        [year]        => 2013
                        [month]        => 7
                        [day]        => 8
                        [hour]        => 16
                        [min]        => 15
                    )
                    [full_moon]    => stdClass Object
                    (
                        [hyear]        => AD 2013
                        [year]        => 2013
                        [month]        => 7
                        [day]        => 23
                        [hour]        => 2
                        [min]        => 59
                    )
                )
            
            합삭/망 정보의 경우, 한달에 음력월이 2개가 있으므로,
            1일의 정보만 얻어서는 합삭/망 중에 1개의 정보만 나올 수 있다.
            따라서, 1일의 데이터를 얻은 다음에 음력 1일의 정보까지
            구하면 한달의 합삭/망 정보를 모두 표현 가능하다.
            예시    :
                $lunar = LunarSolar::get_moon_status ('2013-07-01');
                if($lunar -> large_month)    // 평달의 경우 마지막이 29일, 큰달은 30일이다.
                    $plus = 29 - $lunar -> day;
                else
                    $plus = 30 - $lunar -> day;
                
                $result_1 = LunarSolar::get_moon_status ('2013-07-01');            // 음력 2013-05-23
                $result_2 = LunarSolar::get_moon_status ('2013-07-'.1 + $plus)    // 음력 2013-06-01
        */
        // $date의 형식
        // 1. unixstmap (1970년 12월 15일 이후부터만 가능)
        // 2. Ymd or Y-m-d
        // 3. null date (현재 시간)
        // 4. 1582년 10월 15일 이전의 날짜는 율리우스력의 날짜로 취급
        public static function get_moon_status ($date = null)
        {
            list ($year$month$day= self::convert_to_args ($date);
            list ($year$month$day= self::fix_calendar ($year$month$day);
 
            list (
                $year_start$month_start$day_start$hour_start$min_start,
                $year_mid$month_mid$day_mid$hour_mid$min_mid,
                $year_end$month_end$day_end$hour_end$min_end,
            ) = LunarSolarBase::get_conjunction_full_moon ($year$month$day);
            
            // 합삭(New moon)
            $new_moon = self::convert_date_to_utc_julian (
                sprintf (
                    '%s %s:%s:00',
                    self::get_hyphen_date (array ($year_start$month_start$day_start)),
                    $hour_start < 10 ? '0'.$hour_start : $hour_start,
                    $min_start < 10 ? '0'.$min_start : $min_start
                )
            );
 
            // 1852-10-15 이전이면 율리우스로 변경
            if(self::is_gregorian ($year_start$month_start$day_start=== false)
            {
                $result            = self::convert_gregorian_to_julian (array ($year_start$month_start$day_start));
                $year_start        = $result -> year;
                $month_start    = $result -> month;
                $day_start        = $result -> day;
            }
 
            // 망(Full moon)
            $full_moon = self::convert_date_to_utc_julian (
                sprintf (
                    '%s %s:%s:00',
                    self::get_hyphen_date (array ($year_mid$month_mid$day_mid)),
                    $hour_mid < 10 ? '0'.$hour_mid : $hour_mid,
                    $min_mid < 10 ? '0'.$min_mid : $min_mid
                )
            );
 
            // 1852-10-15 이전이면 율리우스로 변경
            if(self::is_gregorian ($year_mid$month_mid$day_mid=== false)
            {
                $result        = self::convert_gregorian_to_julian (array ($year_mid$month_mid$day_mid));
                $year_mid    = $result -> year;
                $month_mid    = $result -> month;
                $day_mid    = $result -> day;
            }
 
            return (objectarray (
                // 합삭(New moon)
                'new_moon'    => (objectarray (
                    'hyear'        => self::get_readable_year ($year_start),
                    'year'        => $year_start,
                    'month'        => $month_start,
                    'day'        => $day_start,
                    'hour'        => $hour_start,
                    'min'        => $min_start,
                    'julian'    => $new_moon,
                ),
                // 망(Full moon)
                'full_moon'    => (objectarray (
                    'hyear'        => self::get_readable_year ($year_mid),
                    'year'        => $year_mid,
                    'month'        => $month_mid,
                    'day'        => $day_mid,
                    'hour'        => $hour_mid,
                    'min'        => $min_mid,
                    'julian'    => $full_moon,
                ),
            );
        }
 
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
        // get_day_fortune () 메서드의 간지 인덱스 반환값을 이용하여, 간지 값을 반환한다.
        // $ganji_count = get_day_fortune () 메서드의 간지 인덱스 번호
        // $language = 출력모드이며 boolean 형태(false => 한글 간지명, true => 한자 간지명)
        public static function get_ganji_value ($ganji_count$language = false)
        {
            if($ganji_count > 59)
                $ganji_count -= 60;
            
            $mode = $language ? 'HAN_GANJI' : 'KOR_GANJI';
            return LunarSolarBase::$mode[$ganji_count];
        }
    }
?>
cs