我有一个以asc顺序排序的日期数组。我想将日期显示为
Oct 10,12,24 2012
Dec 12,20,24 2012
Jan 02,10,25 2013
我的日期是月份ie.Oct,Dec,Jan ..以及日期和年份,但我希望它以上述格式显示。我已经尝试了下面的代码,但它没有给出所需的结果。有人可以帮我这个吗? $ CruiseDetailsSailing是包含升序日期的数组。
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日。我只想将结果显示为10月仅一次,然后只显示日期和年份。
谢谢,
答案 0 :(得分:1)
这应该做:
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";
}
}
以下输入:
$date = Array('10/03/2012', '10/10/2012', '10/17/2012', '11/04/2012', '11/05/2012', '11/23/2012');
结果:
Oct 03,10,17 2012
Nov 04,05,23 2012
答案 1 :(得分:0)
你可以像这样做
/* 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;
}
}