Can';不要在PHP中添加时间
所以我有这个代码:Can';不要在PHP中添加时间,php,Php,所以我有这个代码: $laikas = htmlspecialchars($_POST['rodymolaikas']); $laikas = date( "H:i:s", strtotime( $laikas ) ); $videotrukme = $_POST['videotrukme']; $videotrukme1 = round($videotrukme); $videotrukme2 = sprintf('%02d:%02d:%02d', ($videotrukme1/
$laikas = htmlspecialchars($_POST['rodymolaikas']);
$laikas = date( "H:i:s", strtotime( $laikas ) );
$videotrukme = $_POST['videotrukme'];
$videotrukme1 = round($videotrukme);
$videotrukme2 = sprintf('%02d:%02d:%02d', ($videotrukme1/3600),($videotrukme/60%60), $videotrukme%60);
$laikas_end = date('H:i:s', strtotime($laikas) + strtotime($videotrukme2));
echo $laikas_end;
变量为偶数:
$laikas = 17:00:00;
$videotrukme2 = 00:01:12;
但我得到的答案是:
laikas_end = 02:00:00
你能告诉我为什么会这样吗?你可以像这样完成时间加法
<?php
$laikas = htmlspecialchars($_POST['rodymolaikas']);
$laikas = date( "H:i:s", strtotime( $laikas ) );
$videotrukme2 = $_POST['rodymolaikas'];
$videotrukme2 = date( "H:i:s", strtotime( $videotrukme2 ) );
$h = strtotime($laikas);
$h2 = strtotime($videotrukme2);
$minute = date("i", $h2);
$second = date("s", $h2);
$hour = date("H", $h2);
$convert = strtotime("+$minute minutes", $h);
$convert = strtotime("+$second seconds", $convert);
$convert = strtotime("+$hour hours", $convert);
$laikas_end = date('H:i:s', $convert);
echo $laikas_end;
?>
取出'$laikas'
并将其放入strotime($laikas)
strotime($laikas')
不会按您期望的方式工作。去掉那些引语。对于strotime(“$videotrukme2”)也一样。然后仔细阅读PHP中单引号和双引号之间的区别,如果你不这样做,它会继续绊倒你。@ceejayoz我删除了引号,然后答案变成06:32:56
,这是因为strotime
的结果将是一个UNIX时间戳(当前UNIX时间戳大约是1496691070
)。把其中两个加在一起,你现在就能知道20世纪60年代的日期了。打印出你的值并进行相应的调试。@ceejayoz哦,没想到增加时间会这么难,哈哈。我是PHP新手,所以不知道你在说什么。但是谢谢你的提示,兄弟!