dtgu21994537 2009-10-17 14:39
浏览 16
已采纳

我喜欢70年代,但那是不对的(PHP时间问题)

So I'm storing a hidden-field time() call with each entry to my DB. The only problem is, they all come out as:

16777215

which is from 1970. I can't tell why that number, because it's not the beginning or end of the current timestamp, and it's the same for every entry. It's going into a mediumint(50) MySQL column, and the field looks as so:

    <input type="hidden" name="time" value="<?php echo time(); ?>">

Is it a casting problem? I bet it is. Sunnuva Gun. Let me go check that...

So I changed the column to "Text" and it get's entered accurately. So if I want this to be a number to do math with it, do I need to use php to convert it to an int before I save it? And for just curiosity, why did it choose that number before?

  • 写回答

3条回答 默认 最新

  • doucong8553 2009-10-18 14:04
    关注

    If you are storing dates, I highly suggest using the built in Date types in MySQL. Why? You can do date validation (MySQL will not allow you to enter a Month 13, for example), generate manual queries much easier, and when looking at raw data you know what the date is. MySQL will also allow you to use things like CURRENT_TIMESTAMP() on columns that you need to record the time when a row changes.

    Storing dates as integers does make it much easier to do simple math but I think that is about where the advantage ends in code. PHP has a strong Date/Time library you can use for dealing with dates. If you want to look at the amount of time between two dates, you can use Date::diff. You can add and subtract time from dates as well. (Yes, there are functions to correspond to the objects if you decide to use functions instead of objects.)

    Manual entry for Date/Time Objects

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

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。