在当前日期增加天数[重复]

时间:2022-08-25 17:43:50

This question already has an answer here:

这个问题已经有了答案:

I want to add some days to current date.I use following code for it.

我想在现在的日期上再加几天。我用下面的代码。

$arrSearchValues=1000;

$datToDate = date('Y-m-d', strtotime("+$arrSearchValues days"));

Everything working perfect until the number of days exceeds 9070.Above this number am getting result date "1970-01-01'". Is there any way to overcome this problem ?please help me with example

一切运转正常,直到天数超过9070天。在这个数字之上,我得到的结果日期是“1970-01-01”。有什么办法可以解决这个问题吗?请给我举个例子

3 个解决方案

#1


2  

January 19, 2038 is a special day: on that day, 32-bit date/time implementations based on Unix time run out of bits to represent dates and times, which is why the addition overflows and the result is reset to the start of the Unix epoch (Jan 1 1970).

2038年1月19日是一个特殊的日子:在这一天,基于Unix时间的32位日期/时间实现用字节来表示日期和时间,这就是为什么添加溢出和结果被重新设置为Unix纪元的开始(1970年1月1日)。

To get around this restriction, switch to using DateTime and friends:

要绕过这个限制,切换到使用DateTime和friends:

$d = new DateTime();
$d->modify("+10000 days");
echo $d->format("Y-m-d");

#2


2  

As per http://php.net/manual/en/function.strtotime.php

根据http://php.net/manual/en/function.strtotime.php

The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 UTC to Tue, 19 Jan 2038 03:14:07 UTC. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer.)

时间戳的有效范围通常是从1901年12月13日星期五20:45:54 UTC到2038年1月19日03:14:07 UTC。(这些日期对应一个32位有符号整数的最小值和最大值。)

after 9070 days your timestamp is getting out of range due to which you are not getting the valid timestamp and hence the error

9070天后,您的时间戳超出范围,因为您没有得到有效的时间戳,因此会出现错误

You can show error if not valid timestamp

如果没有有效的时间戳,可以显示错误

$arrSearchValues = 1000;
if(strtotime("+$arrSearchValues days"))
{
   $datToDate = date('Y-m-d', strtotime("+$arrSearchValues days"));
}
else
{
    echo "Out of range";
}

#3


0  

Try this, it should to be the fastest way:

试试这个,它应该是最快的方法:

$arrSearchValues=time()+1000*24*60*60;
$datToDate = date('Y-m-d', $arrSearchValues);

sorry, I forgot to use time(), check updated code.

对不起,我忘记使用time(),请检查更新后的代码。

#1


2  

January 19, 2038 is a special day: on that day, 32-bit date/time implementations based on Unix time run out of bits to represent dates and times, which is why the addition overflows and the result is reset to the start of the Unix epoch (Jan 1 1970).

2038年1月19日是一个特殊的日子:在这一天,基于Unix时间的32位日期/时间实现用字节来表示日期和时间,这就是为什么添加溢出和结果被重新设置为Unix纪元的开始(1970年1月1日)。

To get around this restriction, switch to using DateTime and friends:

要绕过这个限制,切换到使用DateTime和friends:

$d = new DateTime();
$d->modify("+10000 days");
echo $d->format("Y-m-d");

#2


2  

As per http://php.net/manual/en/function.strtotime.php

根据http://php.net/manual/en/function.strtotime.php

The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 UTC to Tue, 19 Jan 2038 03:14:07 UTC. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer.)

时间戳的有效范围通常是从1901年12月13日星期五20:45:54 UTC到2038年1月19日03:14:07 UTC。(这些日期对应一个32位有符号整数的最小值和最大值。)

after 9070 days your timestamp is getting out of range due to which you are not getting the valid timestamp and hence the error

9070天后,您的时间戳超出范围,因为您没有得到有效的时间戳,因此会出现错误

You can show error if not valid timestamp

如果没有有效的时间戳,可以显示错误

$arrSearchValues = 1000;
if(strtotime("+$arrSearchValues days"))
{
   $datToDate = date('Y-m-d', strtotime("+$arrSearchValues days"));
}
else
{
    echo "Out of range";
}

#3


0  

Try this, it should to be the fastest way:

试试这个,它应该是最快的方法:

$arrSearchValues=time()+1000*24*60*60;
$datToDate = date('Y-m-d', $arrSearchValues);

sorry, I forgot to use time(), check updated code.

对不起,我忘记使用time(),请检查更新后的代码。