PHP日期格式化失败,MySQL DATE值

时间:2013-08-28 17:23:39

标签: php

任何人都可以解释为什么以下代码

function convdate($date) {
$formatdate = strtoupper(date('M', $date ));
$formatdate .= " " .date('d',$date). " ";
return $formatdate;
}

$sql = "SELECT * FROM livedates ORDER by date";
$res = mysql_query($sql);

while($livedates = mysql_fetch_array($res)){

   $output= convdate($livedates['date']) . "<br>";
   echo $output;
}

输出:

DEC 31
DEC 31
DEC 31

如果我只是使用

从结果集输出日期
$output= $livedates['date']. "<br>";

我得到了

2013-08-03
2013-08-10
2013-12-09

我想要的是

AUG 03
AUG 10
DEC 09 

这对我来说是一个错误,我意识到这一点,但我很难过!

4 个答案:

答案 0 :(得分:2)

在将$livedates['date']传递给date函数之前,您需要将strtotime转换为unix时间戳。 $formatdate = strtoupper(date('M', strtotime($date) )); 可以做到:

{{1}}

答案 1 :(得分:1)

你的功能有什么问题

日期的第二个参数必须是时间戳,因此这是可以工作的函数

<?php
function convdate($date) {
$date = strtotime($date);
$formatdate = strtoupper(date('M', $date ));
$formatdate .= " " .date('d',$date). " ";
return $formatdate;
}
?>

答案 2 :(得分:0)

尝试将您的功能更改为:

function convdate($date) {
    $formatdate = new DateTime($date);
    return strtoupper($formatdate->format('M d'));
}

使用DateTime课程非常精彩,我建议您查看。

答案 3 :(得分:0)

Your updated code should be as follows    
function convdate($date) {
    $formatdate = strtoupper(date('M', strtotime($date) ));
    $formatdate .= " " .date('d',strtotime($date)). " ";
    return $formatdate;
    }

    $sql = "SELECT * FROM livedates ORDER by date";
    $res = mysql_query($sql);

    while($livedates = mysql_fetch_array($res)){

       $strinoutput= convdate($livedates['date']) . "<br>";
       echo $strinoutput;
    }