PHP 将 15 分钟添加到时间值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20557059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Adding 15 minutes to Time value
提问by user982124
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.
时间采用这种格式 - 9:15:00 - 即上午 9:15。然后我需要为此添加 15 分钟并将其存储在一个单独的变量中,但我很困惑。
I'm trying to use strtotime without success, e.g.:
我正在尝试使用 strtotime 没有成功,例如:
$endTime = strtotime("+15 minutes",strtotime($selectedTime)));
but that won't parse.
但这不会解析。
回答by AbraCadaver
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().
您的代码不起作用(解析),因为最后有一个额外的代码会)导致Parse Error。数一下,你有 2(和 3 )。如果您修复该问题,它会正常工作,但会strtotime()返回时间戳,因此要获得人类可读的时间,请使用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):
在没有任何 TZ 或 DST 的情况下直接进行时间并添加 15 分钟(阅读 zerkms 评论):
$endTime = strtotime($selectedTime) + 900; //900 = 15 min X 60 sec
Still, the )is the main issue here.
尽管如此,这)是这里的主要问题。
回答by Mike Brant
Though you can do this through PHP's time functions, let me introduce you to PHP's DateTimeclass, which along with it's related classes, really should be in any PHP developer's toolkit.
尽管您可以通过 PHP 的时间函数来实现这一点,但让我向您介绍 PHP 的DateTime类,它及其相关类确实应该出现在任何 PHP 开发人员的工具包中。
// note this will set to today's current date since you are not specifying it in your passed parameter. This probably doesn't matter if you are just going to add time to it.
$datetime = DateTime::createFromFormat('g:i:s', $selectedTime);
$datetime->modify('+15 minutes');
echo $datetime->format('g:i:s');
Note that if what you are looking to do is basically provide a 12 or 24 hours clock functionality to which you can add/subtract time and don't actually care about the date, so you want to eliminate possible problems around daylights saving times changes an such I would recommend one of the following formats:
请注意,如果您想要做的基本上是提供 12 或 24 小时时钟功能,您可以添加/减去时间并且实际上并不关心日期,因此您希望消除夏令时更改和因此,我会推荐以下格式之一:
!g:i:s12-hour format without leading zeroes on hour
!g:i:s没有前导零的 12 小时格式
!G:i:s12-hour format with leading zeroes
!G:i:s带前导零的 12 小时格式
Note the !item in format. This would set date component to first day in Linux epoch (1-1-1970)
请注意!格式中的项目。这会将日期组件设置为 Linux 时代的第一天 (1-1-1970)
回答by FirstFraktal
To expand on previous answers, a function to do this could work like this (changing the time and interval formats however you like them according to this for function.date, and this for DateInterval):
为了扩展以前的答案,一个函数来做到这可能是工作像这样(但改变你像他们一样根据时间和间隔格式此为function.date,以及本作DateInterval):
(I've also written an alternate form of the below function here.)
// Return adjusted time.
function addMinutesToTime( $time, $plusMinutes ) {
$time = DateTime::createFromFormat( 'g:i:s', $time );
$time->add( new DateInterval( 'PT' . ( (integer) $plusMinutes ) . 'M' ) );
$newTime = $time->format( 'g:i:s' );
return $newTime;
}
$adjustedTime = addMinutesToTime( '9:15:00', 15 );
echo '<h1>Adjusted Time: ' . $adjustedTime . '</h1>' . PHP_EOL . PHP_EOL;
回答by Nishan
strtotimereturns the current timestamp and dateis to format timestamp
strtotime返回当前时间戳,日期是格式化时间戳
$date=strtotime(date("h:i:sa"))+900;//15*60=900 seconds
$date=date("h:i:sa",$date);
This will add 15 mins to the current time
这将使当前时间增加 15 分钟
回答by Bhavesh Patel
You can use below code also.It quite simple.
你也可以使用下面的代码。它很简单。
$selectedTime = "9:15:00";
echo date('h:i:s',strtotime($selectedTime . ' +15 minutes'));
回答by Khan arman
Current date and time
当前日期和时间
$current_date_time = date('Y-m-d H:i:s');
15 min ago Date and time
15 分钟前 日期和时间
$newTime = date("Y-m-d H:i:s",strtotime("+15 minutes", strtotime($current_date)));

