douzhuangna6906 2011-03-03 21:09
浏览 60
已采纳

如何使用PHP从mysql数据库中检索和显示时间戳?

Currently I'm using this line of code

echo date("F j Y g:i:s", $row[date]);

But it just gives me January 1 1970 2:33:31

I also want it to look normal because if I don't do the date("F j Y g:i:s", at all, all I get is 2011-03-02 23:00:30 which is the correct date, but displayed in a very abnormal way

  • 写回答

2条回答 默认 最新

  • dsf5632 2011-03-03 21:11
    关注

    Typecast to UNIX_TIMESTAMP inside the query:

    SELECT UNIX_TIMESTAMP(YourDateField) FROM YourTable
    

    Since I see responses to the other answer while this is ignored, let me elaborate:

    There are two common date types. Both are actually numbers, that represent a duration since a given date.

    1. One is the number of days (float) since 1900. One is one day. The fraction is the fraction of the day.
    2. Other is the number of seconds since 1970. This can be an int (whole seconds) or a float (including fractional seconds).

    If you got the first date, but treat it like the second format, you arecounting days for seconds. Instead of 111 years since 1900, you're counting 111 seconds since 1970. That explains why you get that date.

    Therefore, use the UNIX_TIMESTAMP function, which will convert the first float notation to a timestamp in seconds. It is needed because that is also the type PHP uses.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度