date_format PHP 使用 mySQL 调用
我在格式化从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试 date()
或直接在 MYSQL 查询中使用 DATE_FORMAT() :
try date()
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
最好只通过 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