php排序数据格式(Y-m-d)

时间:2015-12-02 06:20:56

标签: php sorting

$now = time(); 
$day7 = array();
$day7_srt="";
//get last 7days
for($i=0; $i<7; $i++){
    $d = mktime(0,0,0,date('m',$now),date('d',$now)-$i);
    $day7[$i]["d"] = date('d',$d);
    $day7[$i]["m"] = date('m',$d);
    $day7[$i]["y"] = date('Y',$d);
    $day7[$i]["date"] = date('Y-m-d',$d);
}
sort($day7);

foreach ($day7 as $key => $value) {
    $d = $value['d'];
    $m = $value['m'];
    $day7_srt .= "\"$m-$d\",";
}
$day7_srt=substr("$day7_srt",0,-1); 

echo $day7_srt;

我需要获取最近7天的文字。

如果不排序($ day7),我会得到。

"12-02","12-01","11-30","11-29","11-28","11-27","11-26"

如果使用sort($ day7)

"12-01","12-02","11-26","11-27","11-28","11-29","11-30"

但我需要

"11-26","11-27","11-28","11-29","11-30","12-01","12-02"

1 个答案:

答案 0 :(得分:1)

<?php 
$now = time(); 
$day7 = array();
$day7_srt="";
//get last 7days
for($i=0; $i<7; $i++){
    $d = mktime(0,0,0,date('m',$now),date('d',$now)-$i);
    $day7[$i]["d"] = date('d',$d);
    $day7[$i]["m"] = date('m',$d);
    $day7[$i]["y"] = date('Y',$d);
    $day7[$i]["date"] = date('Y-m-d',$d);
}
asort($day7);


foreach ($day7 as $key => $value) {
    $d = $value['d'];
    $m = $value['m'];
    $day7_srt .= "\"$m-$d\",";
}
$day7_srt=substr("$day7_srt",0,-1); 
$array = explode(",",$day7_srt);
sort($array);
$day7_srt = implode(",",$array);
echo $day7_srt;
?>

O / P

"11-26","11-27","11-28","11-29","11-30","12-01","12-02"