PHP将15分钟添加到时间值

use*_*124 19 php datetime

我有一个接收时间值的表单:

$selectedTime = $_REQUEST['time'];
Run Code Online (Sandbox Code Playgroud)

时间是这种格式 - 9:15:00 - 上午9:15.然后我需要添加15分钟并将其存储在一个单独的变量中,但我很难过.

我试图使用strtotime没有成功,例如:

$endTime = strtotime("+15 minutes",strtotime($selectedTime)));
Run Code Online (Sandbox Code Playgroud)

但那不会解析.

Abr*_*ver 55

您的代码不起作用(解析),因为最后会有一个额外的代码)导致解析错误.伯爵,你有2 (和3 ).如果你修复它会很好,但会strtotime()返回一个时间戳,所以要获得人类可读的时间date().

$selectedTime = "9:15:00";
$endTime = strtotime("+15 minutes", strtotime($selectedTime));
echo date('h:i:s', $endTime);
Run Code Online (Sandbox Code Playgroud)

获取一个编辑器,它将语法突出显示并显示无与伦比的括号,大括号等.

只做直接时间没有任何TZ或DST并添加15分钟(阅读zerkms评论):

 $endTime = strtotime($selectedTime) + 900;  //900 = 15 min X 60 sec
Run Code Online (Sandbox Code Playgroud)

不过,这)是主要问题.


Mik*_*ant 8

虽然你可以通过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');
Run Code Online (Sandbox Code Playgroud)

请注意,如果您要查看的内容基本上是提供12或24小时的时钟功能,您可以添加/减去时间并且实际上并不关心日期,因此您希望消除围绕日光的可能问题,节省时间更改我推荐以下格式之一:

!g:i:s 12小时格式,小时没有前导零

!G:i:s 带有前导零的12小时格式

请注意!格式中的项目.这将在Linux时代(1-1-1970)中将日期组件设置为第一天


Nis*_*n B 5

strtotime返回当前时间戳, date是格式化时间戳

  $date=strtotime(date("h:i:sa"))+900;//15*60=900 seconds
  $date=date("h:i:sa",$date);
Run Code Online (Sandbox Code Playgroud)

这将使当前时间增加 15 分钟