开发者

Displaying Date from MySQL Database

I know this is covered a lot in SO, but i can't find the exact solution i need so please bear with me. When users submit an entry to my database, i'm storing the date and time in a DATETIME field called date.

Now when i come to echo the date their particular entry was posted, i'm u开发者_如何学JAVAsing:

<p>Posted:<br /><?php echo $rsjobinfo['date'];?></p>

Of course this is displaying the full date and time and i only want to display something like 7 July, 2011.

So above my echo statement i've done this:

$date = $rsjobinfo['date'];
$date = date('j F, Y');

Which, if i echo $date, displays today's date in the correct format, but i cannot figure out how to apply that format to the date that's being pulled from the database. If i replace

  <p>Posted:<br /><?php echo $rsjobinfo['date'];?></p>

with

  <p>Posted:<br /><?php echo $date;?></p>

It just echo's todays date (albeit in the format i want), rather than the database date.

Can someone help?

Thanks in advance, Dan


You need to pass the right date to date(). See http://php.net/manual/en/function.date.php and http://php.net/manual/en/function.strtotime.php

$date = date('j F, Y', strtotime($rsjobinfo['date']));


$date = date('j F, Y', strtotime($rsjobinfo['date']));

See the following documentation for more details:

  • strtotime()
  • date()


As other suggested, you should use PHP functions like strtotime() and date():

$date = $rsjobinfo['date'] ;
$dateToPrint = date('j F, Y', strtotime($date) ) ;
echo $dateToPrint ;

You could also use MySQL DATE_FORMAT() function:

$sql = "SELECT ...
             , `date`
             , DATE_FORMAT(`date`, '%e %M, %Y') AS dateToPrint 
        FROM 
            ...
       " ;

$dateToPrint = $rsjobinfo['dateToPrint'] ;
echo $dateToPrint ;
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜