首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以特定格式显示日期

以特定格式显示日期
EN

Stack Overflow用户
提问于 2012-10-11 13:41:50
回答 2查看 953关注 0票数 1

我有一个按asc顺序排序的日期数组。我想将日期显示为

代码语言:javascript
复制
Oct 10,12,24 2012
Dec 12,20,24 2012
Jan 02,10,25 2013

我有月份ie.Oct,12月,1月的日期。以及日期和年份,但我希望它以上述格式显示。我已经尝试了下面的代码,但它没有给出预期的结果。有没有人能帮我一下?$CruiseDetailsSailing是包含日期升序的数组。

代码语言:javascript
复制
  if (count($CruiseDetailsSailing) > 0) {
                            $date = array();
                            $month = array();
                            $Year = array();
                            for ($n = 0; $n < count($CruiseDetailsSailing); $n++) {
                                if ($CruiseDetailsSailing[$n] != "") {
                                    $date[] = date('Y-m-d', strtotime($CruiseDetailsSailing[$n]));
                                }
                            }
                        }
                        sort($date);
                        if (count($date) > 0) {
                            $temp = "";
                            $yeartemp = "";
                            for ($p = 0; $p < count($date); $p++) {
                                $month = date("M", strtotime($date[$p]));
                                $day = date("d", strtotime($date[$p]));
                                $year = date("Y", strtotime($date[$p]));
                                if ($month != $temp) {
                                    $temp = $month;
                                    ?>
                                    <li> <?php
                        echo $temp . " " . $day . ", ";
                    } else {
                        echo $day . ", ";
                    }
                    if (($p != 0) && ((date("M", strtotime($date[$p]))) == (date("M", strtotime($date[$p - 1])))) && ((date("Y", strtotime($date[$p]))) == (date("Y", strtotime($date[$p - 1]))))) {
                        echo $year;
                    }
                    if (($p != 0) && ((date("M", strtotime($date[$p]))) != (date("M", strtotime($date[$p - 1])))) && ((date("Y", strtotime($date[$p]))) != (date("Y", strtotime($date[$p - 1]))))) {
                        echo $year;
                    }
                }

10月是月份,10,12,24是月份,2012是年份。我得到的日期是2012年10月10日,2012年10月12日,2012年10月24日。我只想将结果显示为Oct一次,然后显示日期和年份一次。谢谢,

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-10-11 14:10:53

这应该可以做到:

代码语言:javascript
复制
sort($date); // sorted input array containing dates

$result_dates = Array();

// bring dates in correct format
foreach($date as $current_date) {
  $month = date("M", strtotime($current_date));
  $day = date("d", strtotime($current_date));
  $year = date("Y", strtotime($current_date));
  $result_dates[$year][$month][] = $day;
}

// output dates
foreach($result_dates as $year => $months) {
  foreach($months as $month => $days) {
    echo $month . " " . implode(",", $days) . " " . $year . "\n";
  }
}

以下输入:

代码语言:javascript
复制
$date = Array('10/03/2012', '10/10/2012', '10/17/2012', '11/04/2012', '11/05/2012', '11/23/2012');

结果如下:

代码语言:javascript
复制
Oct 03,10,17 2012
Nov 04,05,23 2012
票数 1
EN

Stack Overflow用户

发布于 2012-10-11 14:20:56

你可以像这样简单地做

代码语言:javascript
复制
/* Array to pass the function   
$date_array =   array(
                '2012-10-10',
                '2012-12-10',
                '2012-24-10'
                );
*/

function getMyDateFormat($date_array){

    $days   =   array();
    $year   =   '';
    $month  =   '';

    sort($date_array);  

    if(count($date_array)>0){


        foreach($date_array as $row)
        {
            $days[] =   date('d',strtotime($row));
            $year   =   date('Y',strtotime($row));
            $month  =   date('M',strtotime($row));
        }

        $new_date   =   $month  .' '.   implode(',',$days)  .' '.   $year;
        return  $new_date;
    }
}   
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12832924

复制
相关文章

相似问题

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