gpt4 book ai didi

php - 混淆添加 2 个时间值

转载 作者:行者123 更新时间:2023-11-29 01:18:39 26 4
gpt4 key购买 nike

基本上,这些事情一直让我很沮丧,我是编程新手,所以如果我问了一个愚蠢的问题,我深表歉意。

我的数据库中存储了一个 MySQL time()。我想将这个时间添加到当前时间以建立目标时间。

$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/>";

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

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

有人可以为我解释一下吗

非常感谢,

最佳答案

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();

关于php - 混淆添加 2 个时间值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5718361/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com