在 PHP 中创建等于当前时间减去一小时的变量

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

Create Variable in PHP Equal to Current Time Minus One Hour

php

提问by John

In PHP, how could I create a variable called $livetimethat equals the current time minus 1 hour?

在 PHP 中,如何创建一个名为$livetime等于当前时间减去 1 小时的变量?

回答by Jason McCreary

Another way - without all the math and, in my opinion, reads better.

另一种方式 - 没有所有的数学,在我看来,阅读更好。

$hour_ago = strtotime('-1 hour');

回答by pkavanagh

If you're looking for how to display the time in a human readable format, these examples will help:

如果您正在寻找如何以人类可读的格式显示时间,这些示例将有所帮助:

$livetime = date('H:i:s', time() - 3600); // 16:00:00
$livetime = date('g:iA ', time() - 3600); // 4:00PM

回答by Nemoden

$livetime = time() - 3600; // 3600 seconds in 1 hour : 60 seconds (1 min) * 60 (minutes in hour)

See timePHP function for more information.

请参阅时间PHP 函数以获取更多信息。

回答by Avinash Borse

convert your date to strtotime and then subtract one hour from it

将您的日期转换为 strtotime,然后从中减去一小时

$now =  date('Y-m-d H:i:s');
$time   = strtotime($now);
$time   = $time - (60*60); //one hour
$beforeOneHour = date("Y-m-d H:i:s", $time);

回答by JamLizzy101

You could use the date_create function along with the date_sub function like I have shown here below: -

您可以将 date_create 函数与 date_sub 函数一起使用,如下所示:-

$currentTime = date_create(now());
$modifyTime = date_sub($currentTime,date_interval_create_from_date_string("1 hour"));
$liveTime = $modifyTime->format('Y-m-d H:i:s');

回答by MitMaro

Assuming that a timestamp is fine you can use the timefunction like so

假设时间戳很好,您可以time像这样使用该函数

<?php
$livetime = time() - 60 * 60;

回答by AJ.

Here you go:

干得好:

<?php

$now = time();
echo strftime("%c",$now) . "\n";
$livetime = $now-3600;
echo strftime("%c",$livetime) . "\n";
?>

回答by Tadeck

Current time is equal to time()(current time given in seconds after Unix epoch).

当前时间等于time()(Unix 纪元后的当前时间以秒为单位)。

Thus, to calculate what you need, you need to perform calculation: time() - 60*60(current time in seconds minus 60 minutes times 60 seconds).

因此,要计算您需要的内容,您需要执行计算:(time() - 60*60以秒为单位的当前时间减去 60 分钟乘以 60 秒)。

$time_you_need = time() - 60*60;

回答by kaleazy

First convert hours into seconds (3600) then use the following:

首先将小时转换为秒 ( 3600) 然后使用以下内容:

$your_date = date('F jS, Y',time() - 3600);