dongqufi82315 2012-12-11 14:53
浏览 47
已采纳

两次均匀分布值

I have the following code. I need to spread values out evenly between two times: now + 20 minutes to 15:30. The following code is a part of a loop that I did not include for the sake of brevity.

$numlines = count($data);

$calltime = date('U', strtotime('+20 minutes');

$to = date('U', strtotime('15:30'));
$from = $calltime;

$diffinsec = $to - $from;

$apptseparation = ceil($diffinsec / $numlines);

$calltime = date('H:i', strtotime('+' . $apptseparation . ' seconds', $calltime);

What I'm trying to do here is take the current time plus 20 minutes, convert it to a Unix epoch, and subtract it from 15:30 on the same day. The result should be the number of seconds between the two times.

I want to then divide the number of seconds by the total number of lines in my input file, giving me the total number of seconds between each value. Finally, I want to add this number to the existing $calltime variable so each iteration of the loop will add the new time.

What I have doesn't work, though. Date math always confuses me, and in PHP doubly so. If you need any more information, please let me know!

  • 写回答

1条回答 默认 最新

  • doushihu5475 2012-12-11 17:09
    关注

    Well, what are the result you get and the result you expect for? This code seems to work if I well understood:

        $calltime = strtotime('+20 minutes');
        $from = $calltime;
        $to = strtotime('15:30');
    
        $diffinsec = $to - $from;
        if ( $diffinsec < 0 ) { // If it is 18:04 per example
            $diffinsec *= -1;
        }
        $apptseparation = ceil($diffinsec / $numlines);
    
        $calltime = date('H:i', strtotime('+'.$apptseparation.' seconds', $calltime));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持