Php 如何从开始时间减去15分钟?

Php 如何从开始时间减去15分钟?,php,Php,我试着从另一个时间(开始时间)减去15分钟。 我想检查当前时间是否为会议开始前15分钟 foreach($result->value as & $value) { $start = $value->Start->DateTime; $startmeeting = substr($start, 11, -11); //cut time to hour:minute $now= date('H:i', time()); $min= s

我试着从另一个时间(开始时间)减去15分钟。 我想检查当前时间是否为会议开始前15分钟

foreach($result->value as & $value) { 
    $start = $value->Start->DateTime; 
    $startmeeting = substr($start, 11, -11); //cut time to hour:minute

    $now= date('H:i', time());

    $min= strtotime('-15 minutes'); 
    $timebefor = date($startmeeting, $min); //Here I want to substract starttime with 15 min

    if( $now >= $timebefor && $now <= $startmeeting )
    {
        //Show yellow warning box
    }
}
foreach($result->value as&$value){
$start=$value->start->DateTime;
$startmeeting=substr($start,11,-11);//将时间缩短为小时:分钟
$now=日期('H:i',time());
$min=strottime(“-15分钟”);
$timebefor=date($startmeeting,$min);//这里我想用15分钟减去starttime

如果($now>=$timebefor&&$now您基本上有您的解决方案,但是它不整洁,并且包含bug。我想您应该这样做:

foreach ($result->value as $value) { 
    $meetingStart = strtotime($value->Start->DateTime);
    if (($meetingStart > time()) && 
        ($meetingStart < strtotime('15 minutes'))) 
    {
        //Show yellow warning box
    }
}
foreach($result->value as$value){
$meetingStart=strottime($value->Start->DateTime);
如果($meetingStart>time())&&
($meetingStart
简单地说:如果会议是在未来举行的,但距离未来不到15分钟,您必须显示黄色警告框


编程时,请始终注意您选择的名称。请注意我如何使用
$nowPlus15Minutes
,它清楚地指示了该变量包含的内容。您使用了
$min
,这不是很自明的。类似于
$value
$start
的名称也存在同样的问题。可能
$timebefor
是否拼写错误?

我建议您使用PHP:DateTime。如果您的系统使用外部API(如谷歌日历),我通常也会指定时区

$currentTime = new DateTime("now", new DateTimeZone("Asia/Singapore"));
$reminderTime = new DateTime("2019-08-08T12:00:00.0000000", new DateTimeZone("Asia/Singapore"));
$reminderTime->sub(new DateInterval("PT15M")); // PT means period time, 15 minutes.

// Comparison of DateTime is allowed from PHP 5.2.2 onwards
if($currentTime > $reminderTime) {
  // Do something
}

 // For DEBUGGING
 echo $currentTime->format('Y-m-d H:i:s') . "\n" . $reminderTime->format('Y-m-d 
 H:i:s');

有关更多信息,请参阅文档。

在要减去15分钟的位置,
$value->Start->DateTime的值是多少?@Jerodev$Start是2019-08-08T12:00:00.0000000,$startmeeting是12:00,它是DateTime对象吗?@Frankich Timestamp:)不,我的意思是关于
$value->Start->DateTime
,它是对象还是出于某种原因它只是一个字符串?我将用下一个名称来支付更多的附加信息-谢谢:)我用你的代码尝试过,并且一直显示黄色框?@qpA好吧,我确实忘记了以前的会议,也许这就是问题所在?我会添加它。@qpA请再试一次对我来说,问题是我没有真正的方法来测试这段代码……你们没有任何真正的代码要测试,对吧。但你们的解决方案非常好!谢谢:)@qpA我对它进行了一点重构。我认为这样做还是可以理解的。