Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.3k views
in Technique[技术] by (71.8m points)

php mysql 时间计算问题

trade_date 是一个数据表字段,字段类型是datetime

trade_date存储的时间是每隔五分钟存储一条,即:
2020-01-21 14:00:00 2020-01-21 14:05:00 2020-01-21 14:10:00 等等 依次类推~~~
每天存储4个小时候,2020-01-21 09:35:00~2020-01-21 15:00:00

如果现在的数据是2020-01-21 14:00:00 我要计算这个时间130分钟之前的时间
我现在的算法是 strtotime($rt['trade_date'])-130560
怎么把这个结果转换成每5分钟的时间呢 就是跟数据库的2020-01-21 14:00:00 2020-01-21 14:05:00 这些时间进行匹配


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
//初始时间
$str = '2020-01-21 14:05:00';

//130分钟之前
$start = date('Y-m-d H:i:s', strtotime($str.' -130 min'));

//4个小时,每隔5分钟
$count = 4*12;

//循环得到数据
for ($i=0; $i < $count; $i++) { 
    echo date('Y-m-d H:i:s', strtotime($start.' +'.(5*$i).' min'))."<br/>";
}

得到一个基准时间,然后用循环来得到每5分钟的结果
image.png


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...