在 PHP 中为 H:i 格式的时间添加 30 分钟

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2767068/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:37:57  来源:igfitidea点击:

Adding 30 minutes to time formatted as H:i in PHP

phpstrtotime

提问by bateman_ap

Having a nightmare at the moment and just can't see why it isn't working

此刻正在做噩梦,只是不明白为什么它不起作用

I have a value in the form H:i (ie 10:00, 13:30) etc called $time

我有一个 H:i (即 10:00, 13:30) 等形式的值,称为 $time

What I want to do is create two new values, $startTime which is 30 mins before $time and $endTime which is 30 mins after $time

我想要做的是创建两个新值,$startTime 是 $time 之前 30 分钟和 $endTime 是 $time 之后 30 分钟

I have tried the following but just doesn't seem to want to work

我已经尝试了以下但似乎不想工作

$startTime = date("H:i",strtotime('-30 minutes',$time));
$endTime = date("H:i",strtotime('+30 minutes',$time));

If I pass through 10:00 as $time and echo out both $startTime and $endTime I get:

如果我通过 10:00 作为 $time 并回显 $startTime 和 $endTime 我得到:

$startTime = 00:30
$startTime = 01:30        

回答by webbiedave

$time = strtotime('10:00');
$startTime = date("H:i", strtotime('-30 minutes', $time));
$endTime = date("H:i", strtotime('+30 minutes', $time));

回答by Gordon

In order for that to work $timehas to be a timestamp. You cannot pass in "10:00" or something like $time = date('H:i', '10:00');which is what you seem to do, because then I get 0:30 and 1:30 as results too.

为了使其工作$time必须是时间戳。你不能传入 "10:00" 或类似$time = date('H:i', '10:00');你似乎在做的事情,因为那样我也会得到 0:30 和 1:30 作为结果。

Try

尝试

$time = strtotime('10:00');

As an alternative, consider using DateTime (the below requires PHP 5.3 though):

作为替代方案,请考虑使用 DateTime(以下需要 PHP 5.3):

$dt = DateTime::createFromFormat('H:i', '10:00'); // create today 10 o'clock
$dt->sub(new DateInterval('PT30M'));              // substract 30 minutes
echo $dt->format('H:i');                          // echo modified time
$dt->add(new DateInterval('PT1H'));               // add 1 hour
echo $dt->format('H:i');                          // echo modified time

or procedural if you don't like OOP

或者如果你不喜欢 OOP 的程序

$dateTime = date_create_from_format('H:i', '10:00');
date_sub($dateTime, date_interval_create_from_date_string('30 minutes'));
echo date_format($dateTime, 'H:i');
date_add($dateTime, date_interval_create_from_date_string('1 hour'));
echo date_format($dateTime, 'H:i');

回答by Hans

I usually take a slightly different tack:

我通常采取稍微不同的策略:

$startTime = date("H:i",time() - 1800);
$endTime = date("H:i",time() + 1800);

Where 1800 seconds = 30 minutes.

其中 1800 秒 = 30 分钟。

回答by DisgruntledGoat

Your current solution does not work because $timeis a string - it needs to be a Unix timestamp. You can do this instead:

您当前的解决方案不起作用,因为它$time是一个字符串 - 它需要是一个 Unix 时间戳。你可以这样做:

$unix_time = strtotime('January 1 2010 '.$time); // create a unix timestamp
$startTime date( "H:i", strtotime('-30 minutes', $unix_time) );
$endTime date( "H:i", strtotime('+30 minutes', $unix_time) );

回答by donny

$time = 30 * 60; //30 minutes
$start_time = date('Y-m-d h:i:s', time() - $time);
$end_time = date('Y-m-d h:i:s', time() + $time);

回答by Limitless isa

echo date( "Y-m-d H:i:s", strtotime("2016-10-10 15:00:00")+(60*30) );//2016-10-10 15:30:00

or

或者

echo date( "H:i:s", strtotime("15:00:00")+(60*30) ); // 15:30:00

or

或者

echo date( "H:i:s", strtotime(date("H:i:s"))+(60*30) ); // 15:30:00

回答by FirstFraktal

Just 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):

// Return adjusted start and end times as an array.

function expandTimeByMinutes( $time, $beforeMinutes, $afterMinutes ) {

    $time = DateTime::createFromFormat( 'H:i', $time );
    $time->sub( new DateInterval( 'PT' . ( (integer) $beforeMinutes ) . 'M' ) );
    $startTime = $time->format( 'H:i' );
    $time->add( new DateInterval( 'PT' . ( (integer) $beforeMinutes + (integer) $afterMinutes ) . 'M' ) );
    $endTime = $time->format( 'H:i' );

    return [
        'startTime' => $startTime,
        'endTime'   => $endTime,
    ];
}

$adjustedStartEndTime = expandTimeByMinutes( '10:00', 30, 30 );

echo '<h1>Adjusted Start Time: ' . $adjustedStartEndTime['startTime'] . '</h1>' . PHP_EOL . PHP_EOL;
echo '<h1>Adjusted End Time: '   . $adjustedStartEndTime['endTime']   . '</h1>' . PHP_EOL . PHP_EOL;