du970294 2017-02-28 19:12
浏览 95

MySQL PHP每小时入住率

I have a table with time_from and time_to in unix timestamp for a particular object. The time is an occupancy or usage figure, thus the time can span from say 1pm to 3pm.

What I would like to do is make a chart (have that ready in JS) similar to how Google shows store hours "favorite time" in Google Maps, ie. you can see which times there are more/less visitors. This should reflect all entries for the certain object name since the database was started.

I am unsure where to start, because I don't think averaging is the correct way to do this. I expect that if the occupied hours for one entry are 13:30 to 15:30, hours 13, 14, 15 each get +1. If the next entry is 12:00 to 13:40, 12 and 13 get +1.

In this short example, the chart would show 12, 14, 15 with 1 unit, 13 with 2 units.

One more problem arises though. How do I decide when to count an hour as +1? If the time is 15:05 to 16:05 it would make sense not to give 16 a +1. This would require some kind of calculation of minutes in the hour to decide whether or not that hour was "occupied".

How can this be queried from the database to get the correct units?

  • 写回答

1条回答 默认 最新

  • duanliang789262 2017-02-28 19:40
    关注

    I guess you're dealing with table rows meaning

    person x was in the store from 2017-02-28 12:25 to 2017-02-28 14:10

    and similar sorts of information. You have a decision to make about how to score your hours. Here are some alternatives, from which you can pick one.

    1. If the time interval in each sample covers the top of the hour, then you give that hour a +1. For my example row the hours for 13:00 and 14:00 will get +1. This is probably easiest to implement. It will not count people who arrived at 13:10 and left at 13:20.

    2. If the time interval in each sample intersects at all with the time interval from a half-hour before the hour to just under a half-hour after, it gets +1. This counts everybody.

    It's easy and reasonably cheap (comments to the contrary notwithstanding) to convert UNIX timestamps to date / time stamps:

    SELECT FROM_UNIXTIME(1488310427)
    

    gets a time close to when I wrote this.

    SELECT HOUR(FROM_UNIXTIME(1488310427));
    

    gets the hour of the day in which that timestamp occurred. These operations honor the current time zone setting of your MySQL connection. For example, if you want the hour of the day in Atlantic Standard Time, do this.

    SET time_zone='America/Halifax';
    SELECT HOUR(FROM_UNIXTIME(1488310427));
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错