仅在日期和月份之间查找十二生肖

时间:2016-02-18 07:45:02

标签: php

我对以下代码感到非常沮丧,它应该只使用日期和月份来显示婴儿的十二生肖。

$babyDue= date('d-m',strtotime("23-04-2015"));

$ariesStart=date('d-m',strtotime("21-03-2016")); 
$ariesEnd=date('d-m',strtotime("20-04-2016")); 

$taurusStart=date('d-m',strtotime("21-04-2016"));
$taurusEnd=date('d-m',strtotime("20-05-2016"));

$zodiac="thisTextNeedToChange"; 

if(($babyDue>$ariesStart) && ($babyDue<$ariesEnd)) {
    //Set $zodiac to aries
    $zodiac="aries";
    echo $zodiac; 
} elseif (($babyDue>$taurusStart)&& ($babyDue<$taurusEnd)){
    //Set $zodiac to Taurus
    $zodiac="Taurus";
    echo $zodiac; 
}

我认为我的逻辑是正确的,但它没有显示任何内容:(

1 个答案:

答案 0 :(得分:2)

$babyBirthDate = new DateTime('23 April');

$zodiacs = array(
    'Aries' => array(
         new DateTime('21 March'),
         new DateTime('20 April'),
    ),
    'Taurus' => array(
         new DateTime('21 April'),
         new DateTime('20 May'),
    )
);

foreach ($zodiacs as $zodiac => $dateTimeRange) {
    if ($babyBirthDate >= $dateTimeRange[0] && $babyBirthDate <= $dateTimeRange[1]) {
        echo $zodiac;
        break;
    }
}