# php implementation of the sky stem and earth branch calculator example

- 2021-01-25 07:22:49
- OfStack

Heavenly stem and earth branch, referred to as "Ganzhi". In the ancient Chinese calendar, Jia, Yi, Bing, Ding, Wu, Ji, Geng, Xin, Ren and Gui are called "10 Heavenly Stems", while Zi, Chou, Yin, Mao, Chen, Si, Wu, Wei, Shen, You, Xu and Hai are called "102 Earth-branches". The 10 branches and 102 branches were matched in turn to form 610 basic units, and the two worked with each other in a fixed order to form the Gan-Zhi Ji Law. From the oracle bones unearthed in Yin ruins, it can be seen that the Heaven-stem and Earth-branch were mainly used to mark the sun in ancient China, but also used to mark the moon, year and time.

Sky stem and earth branch algorithm 1

Sky stem and earth branch algorithm:

1. Post-AD:

Heavenly stem: A 4 B 5 C 6 D 7 E 8 D 9 G 0 X 1 N 2 C 3

For example, the end number of 1894 is 4 years, and so on

The earth branch: zi 4 Chou 5 Yin 6 MAO 7 Chen 8 si 9 wu 10 wei 11 shen 0 you 1 xu 2 hai 3

Conversion: 1894 divided by 12, the remainder is how many, in the earth branch to find a few

2. BCE:

Heavenly stem: A 7 B 6 C 5 D 4 E 3 D 2 G 1 X 0 N 9 C 8

For example, 7 BC would be a year, and so on

Earth branch: zi -9 Chou -8 Yin -7 MAO -6 Chen -5 si -4 wu -3 wei -2 shen -1 you - 0 xu -11 hai -10

Conversion: for example, in 221 BC, if -221 is divided by 12, the remainder is found in the earth branch

```
<?php
$TGDZ = array (array (' a ', ' b ', ' c ', ' ding ', ' e ', ' f ', ' heptyl ', ' simba ', ' nonyl ', ' decyl ' ),
array (' The child ', ' The ugly ', ' Yin ', ' sockets ', ' Chen" ', ' The third ', ' noon ', ' not ', ' " ', ' unitary ', ' xu-gou ', ' hai ' ) );
$Year = 2014;
$Year_JiSuan = $Year - 1900 + 36;
$TianGanDiZhi = $TGDZ[0][$Year_JiSuan % 10] . $TGDZ[1][$Year_JiSuan % 12];
echo $Year." In the lunar calendar [".$TianGanDiZhi."] years ";
?>
```

Heavenly stem and earth branch algorithm 2

```
<?php
$TGDZ = array (array (' heptyl ', ' simba ', ' nonyl ', ' decyl ' ,' a ', ' b ', ' c ', ' ding ', ' e ', ' f '), array ( ' " ', ' unitary ', ' xu-gou ', ' hai ', ' The child ', ' The ugly ', ' Yin ', ' sockets ', ' Chen" ', ' The third ', ' noon ', ' not ') );
for ($Year = 1900; $Year <= 2099; $Year++)
{
$TianGanDiZhi = $TGDZ[0][$Year % 10] . $TGDZ[1][$Year % 12];
echo $Year . " In the lunar calendar [" . $TianGanDiZhi . "] years <br>";
}
?>
```