【问题标题】:calculate time interval using record saved after each 5 min使用每 5 分钟后保存的记录计算时间间隔
【发布时间】:2015-02-26 07:36:19
【问题描述】:

我在数据库中有记录,例如

id      Time
1       25-02-2015 18:20
2       25-02-2015 18:25
3       25-02-2015 18:30
4       25-02-2015 18:45
5       25-02-2015 18:50
6       25-02-2015 18:55

我想添加时间间隔形式 1 到 3 和 4 到 6, 并且只显示两条记录而不是全部六条。

我尝试过,但对这个简单(可能)的任务感到困惑

foreach($list as $i => $listInfo){
        if(isset($list[$i+1]['dd_data']))
        {
            $nexttime = $list[$i+1]['dd_data'] ; 

            $to_time = strtotime($nexttime);
            $from_time = strtotime($listInfo['dd_data']);
            $diff =  round(abs($to_time - $from_time) / 60,2);
            if($diff<=5)
            {
                $diffsum +=$diff;   
            }
        }else{
                $diffsum = '0'; 
            }


        if($diffsum>=5 && $diff>=5)
        {
        ?>
        <tr class="<?=$class?>">
            <td  class="<?=$leftBotClass?>"><?=$c++?> <?=$diff?></td>               
            <td class="td_br_right left"><?php echo $listInfo['dd_data']; ?> Till next  <?php echo $diffsum; ?> min </td> 
        </tr>
        <?php
        }
    }

谢谢

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 您想在数据中计算 5 分钟的间隔?还是有别的想法?
  • 是的,如果我的网站关闭,我会在每 5 分钟后在数据库中节省时间,例如 25-02-2015 18:20。现在我想要网站保持关闭的时间间隔
  • 意思是你想倒计时 5 分钟,或者更多,是吗?
  • 是的,就像从 25-02-2015 18:20 到 25-02-2015 18:30 和 25-02-2015 18:45 到 25-02-2015 18:55 等等

标签: php mysql datetime


【解决方案1】:

这可能会对您有所帮助:

SELECT t1.id, t1.time, (t2.time - t1.time) AS time_diff
FROM my_table AS t1 INNER JOIN my_table AS t2 ON t2.id = (t1.id + 1)

【讨论】:

    【解决方案2】:

    试试这个代码。我希望这可以帮助你。

    <?php
    
    $array = array(
      array('id'=>'1', 'date'=>'25-02-2015 18:20'),
      array('id'=>'2', 'date'=>'25-02-2015 18:25'),
      array('id'=>'3', 'date'=>'25-02-2015 18:30'),
      array('id'=>'4', 'date'=>'25-02-2015 19:45'),
      array('id'=>'6', 'date'=>'25-02-2015 19:50'),
      array('id'=>'7', 'date'=>'25-02-2015 20:55')
    );
    //your given sample array from database
    
    $new_arr=array();
    $time1=strtotime($array[0]['date']);
    
    foreach($array as $time)
    {
    
    $time2=strtotime($time['date']);
    $diff=$time2-$time1;
    
    if($diff>0 && $diff<600) //check 5(600 second) minute difference
    {
    
        $time1=strtotime($time['date']);
    
    }
    else
    {
    
        $new_arr[]=$time;
        $time1=strtotime($time['date']);
       }
    
      }
    
     foreach ($new_arr as $id => $date) 
     {
       echo $date['date']."<br>"; //display dates
     }
    
     ?>
    

    输出:

      25-02-2015 18:20
      25-02-2015 19:45
      25-02-2015 20:55
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-02
      • 1970-01-01
      • 2020-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多