How to elegantly output the minutes of a specified time?

WBOY
Release: 2016-09-03 00:14:14
Original
951 people have browsed it

The output of the current time is also the number of minutes ending with the specified minute.
For example, 7 14:56:21->14:57:00
14:57:21->15:07:00
It feels like it’s not simple enough to write one myself

function get_minutes($dest){ $start1 =new DateTime(); $date = $start1 ->format('Y-m-d-H-i-s'); list($year,$mon,$day,$hour,$min,$sec) = explode('-',$date); $start2 = new DateTime($year.'-'.$mon.'-'.$day.' '.$hour.':'.$min); $needle = floor($min/10)*10 +$dest; $needle = $needle > $min ? $needle : $needle +10; $extra = $needle - $min; $timestr = '+ '.$extra.' minutes'; $start2->modify($timestr); return $start2->format('Y-m-d H:i:s'); } echo get_minutes(7);
Copy after login
Copy after login

Reply content:

The output of the current time is also the number of minutes ending with the specified minute.
For example, 7 14:56:21->14:57:00
14:57:21->15:07:00
It feels like it’s not simple enough to write one myself

function get_minutes($dest){ $start1 =new DateTime(); $date = $start1 ->format('Y-m-d-H-i-s'); list($year,$mon,$day,$hour,$min,$sec) = explode('-',$date); $start2 = new DateTime($year.'-'.$mon.'-'.$day.' '.$hour.':'.$min); $needle = floor($min/10)*10 +$dest; $needle = $needle > $min ? $needle : $needle +10; $extra = $needle - $min; $timestr = '+ '.$extra.' minutes'; $start2->modify($timestr); return $start2->format('Y-m-d H:i:s'); } echo get_minutes(7);
Copy after login
Copy after login

Let’s analyze the demand first. The required minutes are 7 minutes, 10+7 minutes, 20+7 minutes, 30+7 minutes, 40+7 minutes, 50+7 minutes; the other 57 minutes to the next node is 7 minutes. The difference is 10 minutes.
Because the total number of seconds from 00:00:00 GMT on January 1, 1970 to now.
The first 7 minutes is 420 seconds, and then 600 seconds are added.
The number of seconds that needs to be added is: first divide the current time by 600. If it is less than 420, just use 420 to subtract it. If it is larger, use 600+420 to subtract it.

$time = time(); echo date('Y-m-d H:i:s',$time); $last = $time%600; $last = $last<420?420-$last:1020-$last; echo '
'; echo date('Y-m-d H:i:s',$time+$last);
Copy after login

function get_minutes($dest) { if ($dest >= 10) { throw new Exception('param error!'); } $mitute = date('i'); $unit = $mitute % 10;//个位分钟数 $offset = $dest - $unit; if ($dest <= $unit) { $offset += 10; } return date('Y-m-d H:i:00', strtotime('+' . $offset . ' minutes')); } echo get_minutes(7);
Copy after login

What a strange requirement...
I think your code is a bit complicated, but the idea is actually very simple.
Just check whether the single digit of the minute is greater than 7. If it is greater than 7, use 17-single digit, otherwise Use 7-single digits. What you calculate is the difference between the next minute ending in 7 and the current minute. Then just add this difference, right?

$date = new DateTime(); $minute = $date->format('i'); $diff_minute = $minute[1] >= 7 ? (17 - $minute[1]) : (7 - $minute[1]); $date->add(new DateInterval("PT" . $diff_minute . 'M')); echo $date->format('Y-m-d H:i:s');
Copy after login

Give me an example. For convenience, 7 is hard-coded in it. You can change it according to your needs.

Copy after login
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!