strtotime减去小时和分钟

时间:2017-10-19 07:38:30

标签: php time strtotime

我有这段代码:

<?php 
    $date = date('Y-m-d H:i:s', strtotime('-6 hours', (int)get_the_time('U'))); 
    echo $date; 
?>

正如您所看到的那样,通过减去6个小时来改变时间,我想减去小时数和分钟数,所以我尝试了这个:

<?php 
    $date = date('Y-m-d H:i:s', strtotime('-6 hours', '-23 minutes', (int)get_the_time('U'))); 
    echo $date; 
?>

但是没有工作,任何想法?

提前谢谢

2 个答案:

答案 0 :(得分:5)

strtotime函数只需要2个参数。

int strtotime ( string $time [, int $now = time() ] )

所以你应该尝试以下格式:

$date = strtotime('-6 hours, -43 minutes', $time);

经过测试的代码:

<?php
$time = time();
$date = strtotime('-6 hours', $time);
echo date('d-m-y H:i', $time);
echo date('d-m-y H:i', $date);


$date = strtotime('-6 hours, -43 minutes', $time);
echo date('d-m-y H:i', $time);
echo date('d-m-y H:i', $date);

<强>输出:

19-10-17 00:44
18-10-17 18:44

19-10-17 00:44
18-10-17 18:01

答案 1 :(得分:0)

$time=time();
$datetime_from = date("Y-m-d H:i", strtotime("-43 minutes", strtotime("-6 hours",$time)));