php 从日期减去 6 小时('g:i a', strtotime($time_date_data));

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

subtract 6 hours from date('g:i a', strtotime($time_date_data));

phpdate

提问by Denoteone

I am using the following code to transform a universal time code into something a little more user friendly.

我正在使用以下代码将通用时间代码转换为对用户更友好的代码。

$meeting_time = date('g:i a', strtotime($time_date_data));

But now I need to subtract 6 hours from meeting_time. Should I do it after the code above or can I work it into the same date function?

但是现在我需要从 meeting_time 中减去 6 小时。我应该在上面的代码之后执行还是可以将其处理到相同的日期函数中?

Something like:

就像是:

$meeting_time = date('g:i a' - 6, strtotime($time_date_data));

回答by danielrsmith

$meeting_time = date('g:i a', strtotime($time_date_data) - 60 * 60 * 6);

String-to-time (strtotime) returns a Unix Time Stamp which is in seconds (since Epoch), so you can simply subtract the 21600 seconds, before converting it back to the specified date format.

String-to-time (strtotime) 返回以秒为单位的 Unix 时间戳(自 Epoch 以来),因此您可以简单地减去 21600 秒,然后再将其转换回指定的日期格式。

回答by kevinji

Try this:

尝试这个:

// 6 hours, 3600 seconds in an hour
$meeting_time = date('g:i a', strtotime($time_date_data) - 6 * 3600);

回答by Repox

Another approach:

另一种方法:

$meeting_time = date('g:i a', strtotime('-6 hours', strtotime($time_date_data)));

回答by Sadee

The way of OO PHP:

OO PHP的方式:

$date = new DateTime(); // current time
echo 'Current: '. $date->format('Y-m-d H:i:s') . "\n";
$date->sub(new DateInterval('PT3H55M10S'));
echo $date->format('Y-m-d H:i:s') . "\n";

For sustract 6 hrs:

对于持续 6 小时:

$date = new DateTime('2017-01-20'); // pass $time_date_data to here
$date->sub(new DateInterval('PT6H'));

回答by Jordan

You should be able to do this:

你应该能够做到这一点:

$meeting_time = date('g:i a', strtotime($time_date_data));
date_add($meeting_time, - date_interval_create_from_date_string('6 hours'));