我有$adate;
其中包含:
Tue Jan 4 07:59:59 2011
我想在这个日期添加以下内容:
$duration=674165; // in seconds
添加秒数后,我需要将结果恢复为日期格式。
我不知道我在做什么,但我得到了奇怪的结果。
注意:两个变量都是动态的。现在它们等于给定的值,但下一个查询它们将具有不同的值。
如果您使用的是 php 5.3+,您可以使用一种新方法来完成它。
<?php
$date = new DateTime();
echo $date->getTimestamp(). "<br>";
$date->add(new DateInterval('PT674165S')); // adds 674165 secs
echo $date->getTimestamp();
?>
只需使用一些不错的 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);
鉴于这$adate
是一个时间戳(如果是这种情况),您可以执行以下操作:
$duration = 674165;
$result_date = strtotime(sprintf('+%d seconds', $duration), $adate);
echo date('Y-m-d H:i:s', $result_date);
// add 20 sec to now
$duration = 20;
echo date("Y-m-d H:i:s", strtotime("+$duration sec"));
做这个:
$seconds = 1;
$date_now = "2016-06-02 00:00:00";
echo date("Y-m-d H:i:s", (strtotime(date($date_now)) + $seconds));
$current_time_zone = 150;
date("Y-m-d H:i:s",strtotime(date("Y-m-d H:i:s"))+$current_time_zone);
我为时区制作了这个示例,但是如果您更改某些部分,它可能会对您有所帮助:
$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;
结果:
2018/06/17 3:16:23========2018/06/17 3:15:53
When I need to implement some calculation, I always keep in mind what is it that I need. For example, if I need to add some seconds to a datetime, I need a datetime somewhere in the future. That is, I need a class called Future
. How is it identified? What data does it need to operate? It seems that there should be at least two values: a datetime relative to which I need a future date, and an interval which defines a time distance between start datetime and desired datetime.
So here is a code:
use Meringue\ISO8601DateTime\FromISO8601 as DateTimeFromISO8601String;
$f =
new Future(
new DateTimeFromISO8601String('2011-01-04T07:59:59+00'),
new NSeconds(674165)
);
Then you can output it in ISO8601 format:
$f->value(); // returns 2011-01-12T03:16:04+00:00
If your initial datetime is not in ISO8601 format, which is the case in your example, you should create a datetime from a custom format, hence a name of the class -- FromCustomFormat
. Since it is a datetime which represents itself in ISO8601 format, it extends an abstract class called ISO8601DateTime
. Here is a complete example:
(new Future(
new FromCustomFormat(
'D M j H:i:s Y',
'Tue Jan 4 07:59:59 2011'
),
new NSeconds(674165)
))
->value();
Here is some more about this approach.
我strtotime()
无法解决在当前时间添加动态数据/时间值的问题
这是我的解决方案:
$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
字体:https ://secure.php.net/manual/en/datetime.add.php (也可以参考面向对象的样式,Elzo Valugi解决方案)