我如何将日期分为春秋两类??
我的桌子上有这样的日期
2012-08-27
2011-01-12
2011-08-27
2010-01-12
2010-08-27
2009-01-12
2009-08-27当我从桌子上取回它们时,我需要把它们写成“2010年春天”、“2010年秋季”。
第27天总是秋天,第12天总是春天
我需要提取年份,并检查它是秋天还是春天,并输出像“2010年春天”,“秋季2010”
请帮助我们如何实现这一目标?谢谢。
发布于 2015-03-30 07:59:39
$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));
}发布于 2015-03-30 08:02:02
使用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发布于 2015-03-30 08:06:37
你可以循环的日期和使用爆炸提取日期类似。
foreach($dates as $date){
list($year,$month,$day) = explode('-', $date)
if($day == '27'){
$season = 'FALL '.$year;
}else{
$season = 'SPRING '.$year;
}https://stackoverflow.com/questions/29341035
复制相似问题