dpp42324 2012-10-11 05:41
浏览 30
已采纳

以特定格式显示日期php

I have a date array sorted in asc order. I want to display the date as

Oct 10,12,24 2012
Dec 12,20,24 2012
Jan 02,10,25 2013

I have got the dates as the month ie.Oct,Dec,Jan.. and the dates and year, but i want it to be displayed in the above mentioned format. I have tried the below code, but it is not giving the desired result. Can someone pls help me with this? $CruiseDetailsSailing is the array containing the date in an ascending order.

  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;
                    }
                }

Oct is the month and 10,12,24 are the months and 2012 is the year. I got the the dates as Oct 10 2012,Oct 12 2012,Oct 24 2012. I just want to display the results as Oct only once and then the dates and the year just once. Thanks,

  • 写回答

2条回答 默认 最新

  • dtz30833 2012-10-11 06:10
    关注

    this should do:

    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 . "
    ";
      }
    }
    

    the following input:

    $date = Array('10/03/2012', '10/10/2012', '10/17/2012', '11/04/2012', '11/05/2012', '11/23/2012');
    

    results in:

    Oct 03,10,17 2012
    Nov 04,05,23 2012
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥40 图书信息管理系统程序编写
  • ¥15 7-1 jmu-java-m02-使用二维数组存储多元线性方程组
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题