Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

datetime - PHP Adding 15 minutes to Time value

I have a form that receives a time value:

$selectedTime = $_REQUEST['time'];

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.

I'm trying to use strtotime without success, e.g.:

$endTime = strtotime("+15 minutes",strtotime($selectedTime)));

but that won't parse.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...