date_format PHP 使用 mySQL 调用

发布于 2024-11-29 08:00:18 字数 1508 浏览 4 评论 0原文

我在格式化从 mySQL 数据库返回的日期时遇到问题。 这是我尝试使用的代码:

<?php
function get_gold_time()
{
     $goldquery = mysql_query("SELECT * FROM metal_price WHERE metal= 'GOLD' LIMIT 0, 96"); 


         while($result = mysql_fetch_array( $goldquery )) 
 { 

        echo "'" . date_format($result['time'], 'jS ,g A') . "'" . ", " ;  
 } 

         }
?>

这是我收到的错误:

警告:date_format() 期望参数 1 为 DateTime,第 128 行 /home/bellsnet/public_html/chart.php 中给出的字符串 '',

如果没有日期格式,我的代码看起来像这样:

<?php
function get_gold_time()
{
     $goldquery = mysql_query("SELECT * FROM metal_price WHERE metal= 'GOLD' LIMIT 0, 96"); 


         while($result = mysql_fetch_array( $goldquery )) 
 { 

        echo "'" .$result['time'] . "'" . ", " ;  
 } 

         }
?>

并且返回我的值如下:

'2011-08-10 01:15:02', '2011-08-10 01:00:02', '2011-08-10 00:45:02', '2011-08-10 00:30:02', '2011-08-10 00:15:02', '2011-08-10 00:00:02', '2011-08-09 23:45:03', '2011-08-09 23:30:02', '2011-08-09 23:15:02', '2011-08-09 23:00:03', '2011-08-09 22:45:02', '2011-08-09 22:40:22', '2011-08-09 22:30:02', '2011-08-09 22:15:02', '2011-08-09 22:13:41', '2011-08-10 02:00:02', '2011-08-10 01:45:02', '2011-08-10 01:30:02', '2011-08-10 02:15:02', '2011-08-10 02:30:02', '2011-08-10 02:45:02', '2011-08-10 03:00:02', '2011-08-10 03:15:03', '2011-08-10 03:30:03', '2011-08-10 03:45:03', '2011-08-10 04:00:03', '2011-08-10 04:15:01', 

任何帮助将不胜感激!我已经为此摆弄了好几个小时了。

I am having trouble formating the dates that are being returned from my mySQL database.
Here is the code that I am trying to use:

<?php
function get_gold_time()
{
     $goldquery = mysql_query("SELECT * FROM metal_price WHERE metal= 'GOLD' LIMIT 0, 96"); 


         while($result = mysql_fetch_array( $goldquery )) 
 { 

        echo "'" . date_format($result['time'], 'jS ,g A') . "'" . ", " ;  
 } 

         }
?>

This is the error I receive:

Warning: date_format() expects parameter 1 to be DateTime, string given in /home/bellsnet/public_html/chart.php on line 128
'',

Without the date format my code looks like this:

<?php
function get_gold_time()
{
     $goldquery = mysql_query("SELECT * FROM metal_price WHERE metal= 'GOLD' LIMIT 0, 96"); 


         while($result = mysql_fetch_array( $goldquery )) 
 { 

        echo "'" .$result['time'] . "'" . ", " ;  
 } 

         }
?>

And is returning my values as follows:

'2011-08-10 01:15:02', '2011-08-10 01:00:02', '2011-08-10 00:45:02', '2011-08-10 00:30:02', '2011-08-10 00:15:02', '2011-08-10 00:00:02', '2011-08-09 23:45:03', '2011-08-09 23:30:02', '2011-08-09 23:15:02', '2011-08-09 23:00:03', '2011-08-09 22:45:02', '2011-08-09 22:40:22', '2011-08-09 22:30:02', '2011-08-09 22:15:02', '2011-08-09 22:13:41', '2011-08-10 02:00:02', '2011-08-10 01:45:02', '2011-08-10 01:30:02', '2011-08-10 02:15:02', '2011-08-10 02:30:02', '2011-08-10 02:45:02', '2011-08-10 03:00:02', '2011-08-10 03:15:03', '2011-08-10 03:30:03', '2011-08-10 03:45:03', '2011-08-10 04:00:03', '2011-08-10 04:15:01', 

Any Help WOuld be greatly appreciated! Ive been tinking around with this for hours.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

锦爱 2024-12-06 08:00:18

尝试 date()

<?php
function get_gold_time()
{
     $goldquery = mysql_query("SELECT * FROM metal_price WHERE metal= 'GOLD' LIMIT 0, 96"); 


         while($result = mysql_fetch_array( $goldquery )) 
 { 

        echo "'" . date('jS ,g A', strToTime($result['time'])) . "'" . ", " ;  
 } 

         }
?>

或直接在 MYSQL 查询中使用 DATE_FORMAT() :

try date()

<?php
function get_gold_time()
{
     $goldquery = mysql_query("SELECT * FROM metal_price WHERE metal= 'GOLD' LIMIT 0, 96"); 


         while($result = mysql_fetch_array( $goldquery )) 
 { 

        echo "'" . date('jS ,g A', strToTime($result['time'])) . "'" . ", " ;  
 } 

         }
?>

or use DATE_FORMAT() directly in MYSQL query:

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format

谁把谁当真 2024-12-06 08:00:18

最好只通过 DATE_FORMAT() 格式化 MySQL 中的日期,因为它会快得多并且可以满足您的需求。

请参阅:http:// dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format

It would be far better to just format the date in MySQL through DATE_FORMAT() as it would be quite a bit faster and give you what you want.

See this: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文