简体   繁体   中英

Why is my MySQL date displaying as blank in PHP?

One of the fields in my database is of 'DATE' Type (field-name = 'post_date').

after fetching an array

$record = mysql_fetch_array($queryResult);

if I do

echo $record['post_date'];

I do not get any output (blank).

What should I do to properly handle the date type?

如果执行此操作后得到的输出为空白(并且查询未中断),则问题在于列值为NULL,而不是PHP交互有任何问题。

This should work. Are you sure the fieldname is right, and that there's data in there?

As an aside: Dates are handled differently in MySQL and PHP:

  • PHP stores dates as unix time, ie number of seconds since 00:00:00 on Jan 1st, 1970.
  • MySQL stores them as Strings, like humans read them.

You can use the MySQL from_unixtime() and unix_timestamp() functions to convert back and forth as required.

将MySQL DATE或DATETIME字段解析为UNIX时间戳的最简单方法是使用strtotime

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM