我在编写从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',
任何帮助都将非常感谢!我一直在调整这几个小时。
答案 0 :(得分:1)
尝试日期()
<?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():
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
答案 1 :(得分:0)
通过DATE_FORMAT()在MySQL中格式化日期会好得多,因为它会更快,并为您提供所需的内容。
请参阅:http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format