0

can anyone explain why the following code

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;
}

Outputs :

DEC 31
DEC 31
DEC 31

when if i just output the date from the resultset using

$output= $livedates['date']. "<br>";

I get

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

What I want to be getting is

AUG 03
AUG 10
DEC 09 

Its going to be a mistake on my part , I realise that but i'm stumped !

4

4 に答える 4

2

$livedates['date']関数に渡す前に、UNIX タイムスタンプに変換する必要がありdateます。strtotime出来る:

$formatdate = strtoupper(date('M', strtotime($date) ));
于 2013-08-28T17:39:18.473 に答える
1

間違っているのはあなたの機能です

date の 2 番目のパラメーターはタイムスタンプでなければならないため、これが機能する関数です。

<?php
function convdate($date) {
$date = strtotime($date);
$formatdate = strtoupper(date('M', $date ));
$formatdate .= " " .date('d',$date). " ";
return $formatdate;
}
?>
于 2013-08-28T17:39:20.580 に答える
0

関数を次のように変更してみてください。

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

このDateTimeクラスは驚くほど使いやすいので、ぜひチェックしてみてください。

于 2013-08-28T17:41:39.527 に答える
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;
    }
于 2013-08-28T17:51:59.730 に答える