我需要使用php更改日期格式 [英] I need to change the date format using php

查看:118
本文介绍了我需要使用php更改日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过一个有关此问题的问题,并试图将我的代码修改为类似的事情.基本上我在mysql数据库中有一个字段,该字段的日期格式为yyyy-dd-mm,我需要将该格式设置为dd-mm-yyyy.当前,我的代码被卡在echo "<td><strong> ("d/m/y",". $row['date'] .")</strong></td>";行上,并读取以下Parse错误:语法错误,意外的T_STRING,期望为','或';'.不知道如何解决这个问题,我将不胜感激.

Hi I have looked at a question already posted about this and tried to adapt my code to do a simlar thing. Basicly I have a field in my mysql database that has the date in this format yyyy-dd-mm i need to the format to be dd-mm-yyyy. Currently my code is getting stuck on the line echo "<td><strong> ("d/m/y",". $row['date'] .")</strong></td>"; and reads this Parse error: syntax error, unexpected T_STRING, expecting ',' or ';'. Not sure how to fix this any ideas I would be greatful.

$result = mysql_query("SELECT * FROM dbGigs WHERE CURDATE() < date ORDER BY date");

echo "<table class=\"gigs\">
<tr>
<th>Venue</th>
<th>Town</th>
<th>Date</th>
<th>Time</th>
<th>Status</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td><strong>" . $row['venue'] . "</strong></td>";
  echo "<td><strong>" . $row['town'] . "</strong></td>";
  echo "<td><strong> ("d/m/y",". $row['date'] .")</strong></td>";
  echo "<td><strong>" . $row['time'] . "</strong></td>";
  echo "<td><strong>" . $row['status'] . "</strong></td>";
  echo "</tr>";

推荐答案

第一件事

  echo "<td><strong> ("d/m/y",". $row['date'] .")</strong></td>";

这行看起来不对;我怀疑应该阅读类似的内容

This line looks wrong; I suspect it ought to read something like

  echo "<td><strong>" . date( "d/m/y", $row['date'] ) . "</strong></td>";

请注意缺少的函数调用和串联运算符.顺便说一句,尽管从语法上讲这是正确的(据我所知),但它可能无法满足您的要求,具体取决于$row['date']实际包含的内容-它必须是Unix时间戳,date()才能使用它正确地.

Note the missing function call and concatenation operators. Incidentally, while this is syntactically correct (so far as I can see), it may not do what you're looking for, depending upon what $row['date'] actually contains - it needs to be a Unix timestamp for date() to grok it properly.

要以Unix时间戳获取日期,可以使用MySQL函数

To obtain the date as a Unix timestamp, you can use the MySQL function UNIX_TIMESTAMP() when selecting the date, e.g.

SELECT *, UNIX_TIMESTAMP(date) AS date_ts FROM ...

然后您可以照常访问$row['date_ts'],并将其传递给date()函数进行格式化.另外,您可以使用 strtotime() 解析返回的当前值,以便从中获取时间戳.

You can then access $row['date_ts'] as normal, and pass it into the date() function for formatting. Alternatively, you can use something like strtotime() to parse the current value being returned in order to get a timestamp from that.

另一种替代方法是让MySQL使用其

A further alternative is to have MySQL format the date on your behalf, using its DATE_FORMAT() function; again, you can access this value returned from the query and print it out.

这篇关于我需要使用php更改日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆