我需要根据月份位置对数月(字母表月)进行排序,而不是按字母顺序排列。我的脚本返回一个事件列表,并根据years =>构建一个多关联数组。几个月2012 =>数组(“八月”,“三月”,“九月”),2013 =>阵列( “四月”, “月”);它完美地构建了数组,但我现在需要对月份进行排序,以便它返回2012 =>数组(“March”,“August”,“September”),2013 => ( “四月”,“月);
这是我目前使用的脚本。请注意,mysql中的日期字段设置为DATE而不是VARCHAR,并输入日期YYYY-MM-DD
public function returnMonthsArray() {
$sql = "SELECT `date` FROM `events` WHERE `status`=1 ORDER BY `name`";
$results = $this->db->returnMultiAssoc($sql);
$navArray = array();
foreach($results as $key => $value) {
$timestamp = strtotime($value["date"]);
$year = date("Y",$timestamp);
$month = date("F",$timestamp);
if(!array_key_exists($year, $navArray)) {
$navArray[$year] = array();
}
if(!in_array($month,$navArray[$year])) {
$navArray[$year][] = $month;
}
}
if(count($navArray)) {
return $navArray;
} else {
return false;
}
}
答案 0 :(得分:1)
答案 1 :(得分:1)
尝试此查询
SELECT `date` FROM `events`
WHERE `status`=1 ORDER BY `name`"
GROUP BY MONTH(`date`)