0

我不知道如何在从 MYSQL 中提取所有键的数组中仅隔离 Date 变量。我想让它格式化为 %M %d。我怎样才能把它从 $dataFields 数组中拉出来修改它然后放回去?关键是“日期”。

include('include/connect.php');

$table='currentJobs';

$sql="SELECT * FROM `".$table."`";
$result=mysql_query($sql,$dbLink);

$data=array();
$d=0;
while($row=mysql_fetch_assoc($result)){
    $dataFields=array();

    while(list($key,$value)=each($row)){
        $dataFields[]='<td class="'.$key.'" >'.$row[$key].'</td>';
        if ($d==0){
            $dataKeys[]='<th class="'.$key.'" >'.$key.'</th>';
        }
    }

    if ($d==0){
        $data[$d]='<tr>'.join('',$dataKeys).'<th class="actions">Actions</th></tr>';
        $d++;
    }   

    $data[$d]='<tr>'.join('',$dataFields).'<td>
        <a href="update.php?id='.$row['id'].'">Update</a></td></tr>';
    $d++;
}
mysql_free_result($result);

if ($d>0){
    echo '<table class="sortable">'.join('',$data).'</table>';
}
else {
    echo 'Sorry, data was not found';
}

mysql_close($dbLink);

目前正在显示:

Date    Name    Phone   Bike_Year   Bike_Model  Current_Status  Service_Type ▴  Mechanic    Revenue Notes   Actions
0000-00-00  Test User   206-555-5555    2001    FLHRI   Checked In  Spec Service Interval   Johhny  840.30      Update
4

2 回答 2

0
$sql="SELECT 
    date_format(date, '%M %d') AS date,
    Name,Phone,Bike_Year,Bike_Model,Current_Status  Service_Type,Mechanic,Revenue,Notes
FROM `".$table."`";
于 2013-04-16T03:17:10.540 回答
0

这种方法效率低于 JOE LEE 建议的仅修改 SQL 中的格式。但无论哪种方式都有效,无论您喜欢哪种方式都应该有效。

if($key == 'Date'){
    $dataFields[]='<td class="'.$key.'" >'.date('M d',strtotime($row[$key])).'</td>';
} else {
    $dataFields[]='<td class="'.$key.'" >'.$row[$key].'</td>';
}

或紧凑:

$dataFields[] = $key == 'Date' ? date('M d',strtotime($row[$key])) : $row[$key];

在此处查找日期格式选项: http ://us1.php.net/manual/en/function.date.php

于 2013-04-16T03:14:36.723 回答