PHP:向日期添加秒数

发布于 2024-10-09 03:39:35 字数 275 浏览 6 评论 0原文

我有 $adate; 其中包含:

Tue Jan 4 07:59:59 2011

我想向该日期添加以下内容:

$duration=674165; // in seconds

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

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

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

I have $adate; which contains:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

九八野马 2024-10-16 03:39:35

如果您使用 php 5.3+,您可以使用一种新的方法来做到这一点。

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

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

<?php 
$date = new DateTime();
echo $date->getTimestamp(). "<br>";
$date->add(new DateInterval('PT674165S')); // adds 674165 secs
echo $date->getTimestamp();
?>
流年里的时光 2024-10-16 03:39:35

只需使用一些不错的 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);

Just use some nice PHP date/time functions:

$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);
红ご颜醉 2024-10-16 03:39:35

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

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

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

$duration = 674165;
$result_date = strtotime(sprintf('+%d seconds', $duration), $adate);
echo date('Y-m-d H:i:s', $result_date);
靖瑶 2024-10-16 03:39:35
// add 20 sec to now
$duration = 20;
echo date("Y-m-d H:i:s", strtotime("+$duration sec"));
// add 20 sec to now
$duration = 20;
echo date("Y-m-d H:i:s", strtotime("+$duration sec"));
檐上三寸雪 2024-10-16 03:39:35

这样做:

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

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

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));
过度放纵 2024-10-16 03:39:35
$current_time_zone = 150;
date("Y-m-d H:i:s",strtotime(date("Y-m-d H:i:s"))+$current_time_zone);
$current_time_zone = 150;
date("Y-m-d H:i:s",strtotime(date("Y-m-d H:i:s"))+$current_time_zone);
彼岸花似海 2024-10-16 03:39:35

使用 DateTime::modify 会更容易

(new DateTime($str))->modify("+$duration seconds"); //$str is the date in string

It would be easier with DateTime::modify

(new DateTime($str))->modify("+$duration seconds"); //$str is the date in string
旧街凉风 2024-10-16 03:39:35

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

$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

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
把时间冻结 2024-10-16 03:39:35

我无法使用 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

font: https://secure.php.net/manual/en/datetime.add.php (请参阅面向对象风格,Elzo Valugi 解决方案)

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

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

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 Valugi solution)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文