开发者

SQL and PHP datetime...most direct method

I'm pulling from a datetime formatted field with mysql. I'm grabbing it with PHP, and I'm echoing it. I need to change the format so it is human readable. Ideally, I'd like to do this in PHP so I only need to change a single PHP file. If I do it in SQL, it'll effect multiple files.

Right now the output looks like this: 2011-05-30 21:28:37. I need it to look 开发者_开发技巧like this: 05/30/2011.

Here's my code:

$query = "SELECT `fileName`, 
                 `imageName`,  
                 `imageCaption`, 
                 `imageDate` 
            FROM `images` 
           WHERE `gallery` = '$gallery' 
             AND `imageOrder` = '$featuredImage'";

$result = mysql_query($query);
$num = mysql_numrows($result);

$i = 0;
while ($i < $num) {
  $fileName = mysql_result($result,$i,"fileName");
  $order = mysql_result($result,$i,"imageOrder");
  $imageName = mysql_result($result,$i,"imageName");
  $imageCaption = mysql_result($result,$i,"imageCaption");
  $date = mysql_result($result,$i,"imageDate");

  echo "$date";

  $i++;
}


$time = strtotime(mysql_result($result,$i,"imageDate"));
$display_date = date('d/m/Y', $time);


You need to first convert the datetime field to a timestamp then pass it to PHP's date function to format it.

$date_to_time = strtotime(mysql_result($result,$i,"imageDate"));
$date = date('d/m/Y',$date_to_time);


This should work for you:

$date = mysql_result($result,$i,"imageDate");
$objDate = DateTime::createFromFormat('Y-m-d H:i:s', $date);
echo $objDate->format('m/d/Y');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜