首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我怎么能把日期分为春秋两类呢?

我怎么能把日期分为春秋两类呢?
EN

Stack Overflow用户
提问于 2015-03-30 07:56:25
回答 3查看 52关注 0票数 0

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

我的桌子上有这样的日期

代码语言:javascript
复制
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”

请帮助我们如何实现这一目标?谢谢。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-03-30 07:59:39

代码语言:javascript
复制
$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));
}
票数 0
EN

Stack Overflow用户

发布于 2015-03-30 08:02:02

使用mysql,您可以作为

代码语言:javascript
复制
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
票数 0
EN

Stack Overflow用户

发布于 2015-03-30 08:06:37

你可以循环的日期和使用爆炸提取日期类似。

代码语言:javascript
复制
foreach($dates as $date){

 list($year,$month,$day) = explode('-', $date)
 if($day == '27'){
   $season = 'FALL '.$year;
 }else{
   $season = 'SPRING '.$year;
 }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29341035

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档