LoginSignup
2
0

More than 5 years have passed since last update.

星座を取得

Posted at

PHPにて星座を取得する。

・まずは星座の番号を取得
・その後星座の番号を星座名に変換

という2ステップで行う。
( DBに星座を数値で保存しておきたいので )

・メイン


$datetime = new \DateTime("2018-03-07");
$zodiac_id = getZodiacId($datetime);
$res = getZodiacName($zodiac_id);
print_r($res);//魚座

・関数


//星座をIDとして取得
function getZodiacId($datetime){

    $m = (int)$datetime->format("n");
    $d = (int)$datetime->format("j");

    $zodiacs = [
        [3, 21,  4, 19],//0
        [4, 20,  5, 20],//1
        [5, 21,  6, 21],//2
        [6, 22,  7, 22],//3
        [7, 23,  8, 22],//4
        [8, 23,  9, 22],//5
        [9, 23, 10, 23],//6
        [10, 24, 11, 22],//7
        [11, 23, 12, 21],//8
        [12, 22,  1, 19],//9
        [1, 20,  2, 18],//10
        [2, 19,  3, 20]//11
    ];

    foreach($zodiacs as $key => $zodiac){
        list($start_m, $start_d, $end_m, $end_d) = $zodiac;
        if(
            ($m === $start_m && $d >= $start_d) ||
            ($m === $end_m && $d <= $end_d)
        ){
            return $key;
        }
    }

    return false;
}

//星座のIDを星座名に変換
function getZodiacName($key){

    $zodiacs = [
        '牡羊座',//0
        '牡牛座',//1
        '双子座',//2
        'かに座',//3
        '獅子座',//4
        '乙女座',//5
        '天秤座',//6
        '蠍座',//7
        '射手座',//8
        '山羊座',//9
        '水瓶座',//10
        '魚座',//11
    ];

    return $zodiacs[$key];

}


以上

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0