如何将日期分为春季和秋季两类?

时间:2015-03-30 07:56:26

标签: php date

如何将日期分为春季和秋季两类?

我在表格中有这样的日期

2012-08-27 
2011-01-12 
2011-08-27  
2010-01-12
2010-08-27
2009-01-12
2009-08-27

当我从表中检索它们时,我需要将它们放在" SPRING 2010"," 2010年秋季"

27日的一天总是意味着FALL,而12日的日子总是意味着SPRING

我需要提取年份并检查它是否会下降或弹出并输出类似于" 2010年春季"," 2010年秋季"

请帮忙说明如何实现这一目标?谢谢。

3 个答案:

答案 0 :(得分:0)

$string = '2012-08-27'; // your initial string

$year = substr($string,0,4); //$year = '2012'
$season = (substr($string,-2)=='27' ? 'FALL' : 'SPRING'); //$season = 'FALL'
$full = $season.' '.$year; //$full = 'FALL 2012'

echo $full; //will output 'FALL 2012'

//as a function
function season_year($n){
    //if 27 = fall
    #return ((substr($n,-2)=='27' ? 'FALL' : 'SPRING').' '.substr($n,0,4));
    //if 27 = spring
    return ((substr($n,-2)=='27' ? 'SPRING' : 'FALL').' '.substr($n,0,4));
}

答案 1 :(得分:0)

使用mysql你可以做到

select 
 case 
   when day(date_field) = '27' then concat('Fall',' ',year(date_field))
   when day(date_field) = '12' then concat('Spring',' ',year(date_field))
 end as `display`
from table_name

答案 2 :(得分:0)

您可以遍历日期并使用explode来提取日期,例如。

foreach($dates as $date){

 list($year,$month,$day) = explode('-', $date)
 if($day == '27'){
   $season = 'FALL '.$year;
 }else{
   $season = 'SPRING '.$year;
 }