在PHP减去5分钟后将当前时间缩短到5分钟的正确方法是什么? 我们需要输出时间字符串,以这种格式的2位小数:
$today = date("dmYHi");
示例:
16:32 - > 16时25分
18:54 - > 18:45
20:04 - > 19时55分
非常感谢!
答案 0 :(得分:4)
试试这个:
$date = new DateTime();
$hours = $date->format('H')
$minutes = (integer) $date->format('i');
// round down to the nearest multiple of 5
$minutes = floor($minutes / 5 ) * 5;
// if $minutes is 0 or 5 we add a trailing 0
if($minutes < 10) {
$minutes = '0' . $minutes;
}
// output
echo $hours . ':' . $minutes;
答案 1 :(得分:2)
$current_time = time();
$time = $current_time - (5 * 60);
$time -= $time % (60 * 5);
print_r( date("H:i", $current_time) . " -> " . date("H:i", $time) );
输出:11:19 - &gt; 11:10
答案 2 :(得分:1)
$time = time();
$time -= $time / 60 % 5 * 5 + 300;
$today = date("dmYHi", $time);
print "$today\n";
print date('H:i', $time);
答案 3 :(得分:0)
试试这个:
$h=date("H");
$m=date("i")-5;
while($m%5!=0){
if($m==0)
$h--;
$m--;
}
echo "$h:$m";
答案 4 :(得分:0)
试试这个:
date_default_timezone_set('Asia/Jerusalem');
//Convert your time to unixtimestamp
$time = strtotime('20:04');
echo date('Y-m-d H:i:s', $time);
//Calculate by 5mins round down
$time = intval($time/300)*300-300;
echo date('Y-m-d H:i:s', $time);
答案 5 :(得分:0)
$hours = date('H');
$minutes = (integer) date('i');
// round down to the nearest multiple of 5
$minutes = floor($minutes / 5 ) * 5 - 5;
// if $minutes is 0 or 5 we add a trailing 0
if($minutes < 10) {
$minutes = '0' . $minutes;
}
$date = date("dmY");
#Variablen maken voor 60 minuten radar loop
$a = 'ref_' . $date . $hours . ($minutes - 55 ) . '.png';
$b = 'ref_' . $date . $hours . ($minutes - 50 ) . '.png';
$c = 'ref_' . $date . $hours . ($minutes - 45 ) . '.png';
$d = 'ref_' . $date . $hours . ($minutes - 40 ) . '.png';
$e = 'ref_' . $date . $hours . ($minutes - 35 ) . '.png';
$f = 'ref_' . $date . $hours . ($minutes - 30 ) . '.png';
$g = 'ref_' . $date . $hours . ($minutes - 25 ) . '.png';
$h = 'ref_' . $date . $hours . ($minutes - 20 ) . '.png';
$i = 'ref_' . $date . $hours . ($minutes - 15 ) . '.png';
$j = 'ref_' . $date . $hours . ($minutes - 10 ) . '.png';
$k = 'ref_' . $date . $hours . ($minutes - 5 ) . '.png';
$l = 'ref_' . $date . $hours . $minutes . '.png';
print $a .'<br />' . $b .'<br />' . $c .'<br />' . $d .'<br />' . $e .'<br />' . $f .'<br />' . $g .'<br />' . $h .'<br />' . $i .'<br />' . $j .'<br />' . $k .'<br />' . $l;
输出:
ref_0604201422-50.png
ref_0604201422-45.png
ref_0604201422-40.png
ref_0604201422-35.png
ref_0604201422-30.png
ref_0604201422-25.png
ref_0604201422-20.png
ref_0604201422-15.png
ref_0604201422-10.png
ref_0604201422-5.png
ref_06042014220.png
ref_060420142205.png
应输出:
ref_060420142110.png
ref_060420142115.png
ref_060420142120.png
ref_060420142125.png
ref_060420142130.png
ref_060420142135.png
ref_060420142140.png
ref_060420142145.png
ref_060420142150.png
ref_060420142155.png
ref_060420142200.png
ref_060420142205.png
谢谢你!