笔记:获取生肖与星座
有趣的需求难得一寻,无聊的代码随处都有。。。
<?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 协议》,转载必须注明作者和本文链接
高认可度评论: