Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

php - PHP获得五分钟的最后增量(PHP Get last increment of five minutes)

I am looking to get the previous increment of five minutes from the current time...

(我希望从当前时间获得之前的五分钟增量...)

Lets say that the current time is 12:07pm UTC

(可以说当前时间是UTC的12:07 pm)

I want to put into a variable 12:05pm UTC

(我想在UTC下午12:05输入一个变量)

What would be an easy way of going about this?

(解决这个问题的简单方法是什么?)

  ask by Jeffrey L. Roberts translate from so

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

1 Answer

0 votes
by (71.8m points)

There's nothing built in that allows you to retrieve 'increments' however you can calculate it with minimal code.

(内置没有任何东西可以让您检索“增量”,但是您可以用最少的代码进行计算。)

The use of modulo here allows you to figure out how far past the most recent 5 minute mark is.

(在这里使用模数,可以算出最近5分钟标记的距离。)

It will never be greater than 5 minutes (300 seconds) and can always be subtracted safely to take you back to the time you want.

(它永远不会超过5分钟(300秒),并且始终可以安全地减去它,以使您回到想要的时间。)

$now = time();
echo $now . PHP_EOL;
$five_minutes = 60*5; // 300
$offset = $now % $five_minutes;
$five_block = $now - $offset;
echo date('Y-m-d H:i:s', $five_block);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...