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条)

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办