PHP:为日期添加秒数

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

PHP: add seconds to a date

phpdateadddurationseconds

提问by ADM

I have $adate;which contains:

我有$adate;其中包含:

Tue Jan 4 07:59:59 2011

I want to add to this date the following:

我想在此日期添加以下内容:

$duration=674165; // in seconds

Once the seconds are added I need the result back into date format.

添加秒数后,我需要将结果恢复为日期格式。

I don't know what I'm doing, but I am getting odd results.

我不知道我在做什么,但我得到了奇怪的结果。

Note: both variables are dynamic. Now they are equal to the values given, but next query they will have different values.

注意:这两个变量都是动态的。现在它们等于给定的值,但下一次查询它们将具有不同的值。

回答by Elzo Valugi

If you are using php 5.3+ you can use a new way to do it.

如果您使用的是 php 5.3+,您可以使用一种新方法来实现。

<?php 
$date = new DateTime();
echo $date->getTimestamp(). "<br>";
$date->add(new DateInterval('PT674165S')); // adds 674165 secs
echo $date->getTimestamp();
?>

回答by DiglettPotato

Just use some nice PHP date/time functions:

只需使用一些不错的 PHP 日期/时间函数:

$adate="Tue Jan 4 07:59:59 2011";
$duration=674165;
$dateinsec=strtotime($adate);
$newdate=$dateinsec+$duration;
echo date('D M H:i:s Y',$newdate);

回答by ncuesta

Given the fact that $adateis a timestamp (if that's the case), you could do something like this:

鉴于这$adate是一个时间戳(如果是这种情况),您可以执行以下操作:

$duration = 674165;
$result_date = strtotime(sprintf('+%d seconds', $duration), $adate);
echo date('Y-m-d H:i:s', $result_date);

回答by user1211927

Do this:

做这个:

$seconds = 1;
$date_now = "2016-06-02 00:00:00";

echo date("Y-m-d H:i:s", (strtotime(date($date_now)) + $seconds));

回答by Kristian

// add 20 sec to now
$duration = 20;
echo date("Y-m-d H:i:s", strtotime("+$duration sec"));

回答by Jayesh Paunikar

$current_time_zone = 150;
date("Y-m-d H:i:s",strtotime(date("Y-m-d H:i:s"))+$current_time_zone);

回答by Carlos Rafael

I have trouble with strtotime()to resolve my problem of add dynamic data/time value in the current time

strtotime()无法解决在当前时间添加动态数据/时间值的问题

This was my solution:

这是我的解决方案:

$expires = 3600; //my dynamic time variable (static representation here)
$date = date_create(date('Y-m-d H:i:s')); //create a date/time variable (with the specified format - create your format, see (1))
echo date_format($date, 'Y-m-d H:i:s')."<br/>"; //shows the date/time variable without add seconds/time
date_add($date, date_interval_create_from_date_string($expires.' seconds')); //add dynamic quantity of seconds to data/time variable
echo date_format($date, 'Y-m-d H:i:s'); //shows the new data/time value

font: https://secure.php.net/manual/en/datetime.add.php(consult Object Oriented style too, the Elzo Valugisolution)

字体:https: //secure.php.net/manual/en/datetime.add.php(也请参阅面向对象的样式,Elzo Valugi解决方案)

(1) https://secure.php.net/manual/en/function.date.php

(1) https://secure.php.net/manual/en/function.date.php

回答by Alexandre Pereira

I made this example for a timezone, but if you change some parts it may help you out:

我为时区制作了这个示例,但是如果您更改某些部分,它可能会对您有所帮助:

$seconds_to_add = 30;
$time = new DateTime();
$time->setTimezone(new DateTimeZone('Europe/London'));
$time2 = $time->format("Y/m/d G:i:s");
$time->add(new DateInterval('PT' . $seconds_to_add . 'S'));
$timestamp = $time->format("Y/m/d G:i:s");
echo $timestamp;
echo '========';
echo $time2;

Result:

结果:

2018/06/17 3:16:23========2018/06/17 3:15:53