2013-06-20 153 views
1
<?php 
$con=mysqli_connect("localhost","*********","***********","loadtracker"); 
if (mysqli_connect_errno()) 
{ 
echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 
$result = mysqli_query($con,"SELECT Date FROM pallettracker GROUP BY Date"); 
$DateList = mysqli_fetch_array($result); 
$ListOfMonths=array('Dec','Nov','Oct','Sep','Aug','Jul','Jun','May','Apr','Mar','Feb','Jan');$ListOfDays = array('31','30','29','28','27','26','25','24','23','22','21','20','19','18','17','16','15','14','13','12','11','10','9','8','7','6','5','4','3','2','1'); 
foreach ($ListOfDays as $value) { 
foreach ($ListOfMonths as $value2) { 
$Yaer = array("'$value2' + '$value' + ',2013'"); 
foreach ($Yaer as $yaerdate) { 
foreach ($DateList as $Dates) { 
    if ($yaerdate == $Dates) 
    { 
     echo $yaerdate; 
     echo '/'; 
    } 
} 
} 
} 
} 
?> 

試圖訂購日期爲2011年1月28日的日期陣列和Decending。然後顯示然後與他們之間的/中間。對於Android應用程序訂購日期列表

+0

的最佳方式訂購日期是YYYYMMDD。您應該使用此格式對它們進行排序並使用簡單的方法將其轉換爲您所需的樣式。 – Rob013

回答

0

您可以讓您的SQL查詢爲您訂購它們。這假定該列是日期格式。如果沒有檢查出這個:SQL ORDER BY date problem

在查詢中排序後,您只需將其轉換爲時間,然後根據需要格式化日期。這可能會給你你正在尋找什麼:

<? 

$datetime = strtotime($row->createdate); 
$mysqldate = date("m/d/y g:i A", $datetime); 

$result = mysqli_query($con,"SELECT Date FROM pallettracker GROUP BY Date"); 

foreach($result as $date){ 
    $datetime = strtotime($result); 
    $date = date("M d, y", $datetime); 
    echo $date . '/'; 
} 

?> 
0

我不明白,爲什麼你使用GROUP BY,當你不使用分組功能。 這裏的更好的方法是DISTINCT

我會以這種方式更新代碼:

$result = mysqli_query($con,"SELECT DISTINCT `Date` FROM `pallettracker` ORDER BY `Date` DESC"); 
while($date = mysqli_fetch_assoc($result) 
{ 
    echo date('',strtotime($date['Date'])).'<br />'; 
} 

重要的是,你的「日期」欄在MySQL的格式爲日期或日期時間