混淆添加2个时间值

buy*_*ies 2 php mysql time

基本上这段时间的东西让我感到沮丧,我是编程的新手,所以如果我问一个愚蠢的问题我会道歉.

我有一个MySQL时间()存储在我的数据库中.我想将此时间添加到当前时间以建立目标时间.

$ duration是06:00:00.(MySQL时间)

$length = strtotime($duration);

    $timestart = time();

    $target = $length + time();

    // merely to check if im getting correct values.

    echo "</br>duration: ".date('G:i:s', $length)."<br/>";
    echo "current time:".date('G:i:s', $timestart)."<br/>";
    echo "target time:".date('G:i:s', $target)."<br/>";
Run Code Online (Sandbox Code Playgroud)

目前我得到了这个(不是6小时增加):

持续时间:6:00:00
当前时间:15:51:44
目标时间:19:51:44

有人可以帮我解释一下.

非常感谢,

Mis*_*cha 5

strtotime将其转换为unix时间戳.它不代表六个小时.它代表今天早上6点.你应该使用秒:

$duration = '06:00:00';
$duration_array = explode(':', $duration);

$length = ((int)$duration_array[0] * 3600) + ((int)$duration_array[1] * 60) + (int)$duration_array[2];
$target = $length + time();
Run Code Online (Sandbox Code Playgroud)