笔记:获取生肖与星座

有趣的需求难得一寻,无聊的代码随处都有。。。

<?php


namespace App\Service;


class ZodiacConstellationService
{
    /**
     * 生肖开始年份
     */
    const ZODIAC_START_YEAR = 2020;

    /**
     * 生肖
     */
    const ZODIAC_MOUSE = '鼠';
    const ZODIAC_CATTLE = '牛';
    const ZODIAC_TIGER = '虎';
    const ZODIAC_HARE = '兔';
    const ZODIAC_DRAGON = '龙';
    const ZODIAC_SNAKE = '蛇';
    const ZODIAC_HORSE = '马';
    const ZODIAC_SHEEP = '羊';
    const ZODIAC_MONKEY = '猴';
    const ZODIAC_CHICKEN = '鸡';
    const ZODIAC_DOG = '狗';
    const ZODIAC_PIG = '猪';


    /**
     * 根据年份获取生肖
     *
     * @param $date
     * @return string
     */
    public function getZodiac(string $date): string
    {
        $zodiac = null;
        $year = (int)date('Y', strtotime($date));
        $x = (self::ZODIAC_START_YEAR - $year) % 12;
        if ($x == 0) {
            return self::ZODIAC_MOUSE;
        }
        if ($x == 11 || $x == -1) {
            return self::ZODIAC_CATTLE;
        }
        if ($x == 10 || $x == -2) {
            return self::ZODIAC_TIGER;
        }
        if ($x == 9 || $x == -3) {
            return self::ZODIAC_HARE;
        }
        if ($x == 8 || $x == -4) {
            return self::ZODIAC_DRAGON;
        }
        if ($x == 7 || $x == -5) {
            return self::ZODIAC_SNAKE;
        }
        if ($x == 6 || $x == -6) {
            return self::ZODIAC_HORSE;
        }
        if ($x == 5 || $x == -7) {
            return self::ZODIAC_SHEEP;
        }
        if ($x == 4 || $x == -8) {
            return self::ZODIAC_MONKEY;
        }
        if ($x == 3 || $x == -9) {
            return self::ZODIAC_CHICKEN;
        }
        if ($x == 2 || $x == -10) {
            return self::ZODIAC_DOG;
        }
        if ($x == 1 || $x == -11) {
            return self::ZODIAC_PIG;
        }

        return $zodiac;
    }


    /**
     * 星座
     */
    const CONSTELLATION_AQUARIUS = '水瓶座';
    const CONSTELLATION_PISCES = '双鱼座';
    const CONSTELLATION_ARIES = '白羊座';
    const CONSTELLATION_TAURUS = '金牛座';
    const CONSTELLATION_GEMINI = '双子座';
    const CONSTELLATION_CANCER = '巨蟹座';
    const CONSTELLATION_LEO = '狮子座';
    const CONSTELLATION_VIRGO = '处女座';
    const CONSTELLATION_LIBRA = '天秤座';
    const CONSTELLATION_SCORPIO = '天蝎座';
    const CONSTELLATION_SAGITTARIUS = '射手座';
    const CONSTELLATION_CAPRICORN = '魔蝎座';

    /**
     * 根据月份日期获取星座
     *
     * @param $date
     * @return string
     */
    public function getConstellation(string $date): string
    {
        $constellation = null;
        $time = strtotime($date);
        $year = (int)date('Y', $time);

        if (strtotime($year . '-01-20') < $time && $time <= strtotime($year . '-02-18') + 24 * 3600) {
            return self::CONSTELLATION_AQUARIUS;
        }
        if (strtotime($year . '-02-19') < $time && $time <= strtotime($year . '-03-20') + 24 * 3600) {
            return self::CONSTELLATION_PISCES;
        }
        if (strtotime($year . '-03-21') < $time && $time <= strtotime($year . '-04-19') + 24 * 3600) {
            return self::CONSTELLATION_ARIES;
        }
        if (strtotime($year . '-04-20') < $time && $time <= strtotime($year . '-05-20') + 24 * 3600) {
            return self::CONSTELLATION_TAURUS;
        }
        if (strtotime($year . '-05-21') < $time && $time <= strtotime($year . '-06-21') + 24 * 3600) {
            return self::CONSTELLATION_GEMINI;
        }
        if (strtotime($year . '-06-22') < $time && $time <= strtotime($year . '-07-22') + 24 * 3600) {
            return self::CONSTELLATION_CANCER;
        }
        if (strtotime($year . '-07-23') < $time && $time <= strtotime($year . '-08-22') + 24 * 3600) {
            return self::CONSTELLATION_LEO;
        }
        if (strtotime($year . '-08-23') < $time && $time <= strtotime($year . '-09-22') + 24 * 3600) {
            return self::CONSTELLATION_VIRGO;
        }
        if (strtotime($year . '-09-23') < $time && $time <= strtotime($year . '-10-23') + 24 * 3600) {
            return self::CONSTELLATION_LIBRA;
        }
        if (strtotime($year . '-10-24') < $time && $time <= strtotime($year . '-11-22') + 24 * 3600) {
            return self::CONSTELLATION_SCORPIO;
        }
        if (strtotime($year . '-11-23') < $time && $time <= strtotime($year . '-12-21') + 24 * 3600) {
            return self::CONSTELLATION_SAGITTARIUS;
        }
        if (strtotime($year . '-12-22') < $time && $time <= strtotime($year . '-01-19') + 24 * 3600) {
            return self::CONSTELLATION_CAPRICORN;
        }

        return $constellation;
    }

}

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 3
陈先生

$symbolic_animals = ['鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪'];
// 因为 2020 是鼠年
$symbolic_animals_index =abs( (year - 2020) % 12)
return $symbolic_animals[$symbolic_animals_index];
1年前 评论
Diego_crazy (楼主) 1年前
Diego_crazy (楼主) 1年前
陈先生

$symbolic_animals = ['鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪'];
// 因为 2020 是鼠年
$symbolic_animals_index =abs( (year - 2020) % 12)
return $symbolic_animals[$symbolic_animals_index];
1年前 评论
Diego_crazy (楼主) 1年前
Diego_crazy (楼主) 1年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!