PHP 将 15 分钟添加到时间值 [英] PHP Adding 15 minutes to Time value
问题描述
我有一个接收时间值的表单:
I have a form that receives a time value:
$selectedTime = $_REQUEST['time'];
时间采用这种格式 - 9:15:00 - 即上午 9:15.然后我需要为此添加 15 分钟并将其存储在一个单独的变量中,但我很困惑.
The time is in this format - 9:15:00 - which is 9:15am. I then need to add 15 minutes to this and store that in a separate variable but I'm stumped.
我正在尝试使用 strtotime 没有成功,例如:
I'm trying to use strtotime without success, e.g.:
$endTime = strtotime("+15 minutes",strtotime($selectedTime)));
但这不会解析.
推荐答案
您的代码不起作用(解析),因为您在末尾有一个额外的 )
导致 解析错误.数一下,你有 2 个 (
和 3 个 )
.如果您修复它,它会正常工作,但是 strtotime()
返回一个时间戳,因此要获得人类可读的时间,请使用 date()
.
Your code doesn't work (parse) because you have an extra )
at the end that causes a Parse Error. Count, you have 2 (
and 3 )
. It would work fine if you fix that, but strtotime()
returns a timestamp, so to get a human readable time use date()
.
$selectedTime = "9:15:00";
$endTime = strtotime("+15 minutes", strtotime($selectedTime));
echo date('h:i:s', $endTime);
获取一个编辑器,该编辑器将语法突出显示并显示不匹配的括号、大括号等.
Get an editor that will syntax highlight and show unmatched parentheses, braces, etc.
在没有任何 TZ 或 DST 的情况下直接进行时间并添加 15 分钟(阅读 zerkms 评论):
To just do straight time without any TZ or DST and add 15 minutes (read zerkms comment):
$endTime = strtotime($selectedTime) + 900; //900 = 15 min X 60 sec
不过,)
是这里的主要问题.
Still, the )
is the main issue here.
这篇关于PHP 将 15 分钟添加到时间值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!