php 向时间戳添加天数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3397577/
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
Adding days to a timestamp
提问by Pherrymason
Giving a starting date, I'm adding four times seven days to get 5 different dates separated exactly a week each one.
给出一个开始日期,我将 7 天相加四次,以得到 5 个不同的日期,每个日期间隔一周。
//$date = '28-10-2010';
$timestamp = mktime( 0, 0, 0, 10, 01, 2010 );
echo "Date=".date( 'd-m-Y', $timestamp )."<br>";
$timestamp += (60*60*24*7);
echo "Date=".date( 'd-m-Y', $timestamp )."<br>";
$timestamp += (60*60*24*7);
echo "Date=".date( 'd-m-Y', $timestamp )."<br>";
$timestamp += (60*60*24*7);
echo "Date=".date( 'd-m-Y', $timestamp )."<br>";
$timestamp += (60*60*24*7);
echo "Date=".date( 'd-m-Y', $timestamp )."<br>";
The code outputs this:
代码输出如下:
Date=01-10-2010 Friday
Date=08-10-2010 Friday
Date=15-10-2010 Friday
Date=22-10-2010 Friday
Date=29-10-2010 Friday
Which as long as I know it's correct. But, see what happens when going through the 2010-10-31 and 2010-11-01
只要我知道它是正确的。但是,看看在经历 2010-10-31 和 2010-11-01 时会发生什么
$timestamp = mktime( 0, 0, 0, 10, 28, 2010 ); [...]
$timestamp = mktime( 0, 0, 0, 10, 28, 2010 ); [...]
Curiously it outputs this:
奇怪的是它输出这个:
Date=28-10-2010 Thursday
Date=03-11-2010 Wednesday
Date=10-11-2010 Wednesday
Date=17-11-2010 Wednesday
Date=24-11-2010 Wednesday
What's happening? Second date should be 04-11-2010! Also, I saw that this "fail" happens every ten years! Is this something related with the daylight savings time? If so, how do I solve it? Is there anything that i am overlooking?
发生了什么?第二个日期应该是 04-11-2010!另外,我看到这种“失败”每十年发生一次!这与夏令时有关吗?如果是这样,我该如何解决?有什么我忽略的吗?
Edit:Ok, I outputed the time, just to see what happens and this is what I got now:
编辑:好的,我输出了时间,只是为了看看会发生什么,这就是我现在得到的:
Date=28-10-2010 Thursday :: 00:00:00
Date=03-11-2010 Wednesday :: 23:00:00
Date=10-11-2010 Wednesday :: 23:00:00
Date=17-11-2010 Wednesday :: 23:00:00
Date=24-11-2010 Wednesday :: 23:00:00
Seems something related with the time, something happens at 2010-11-31...
似乎与时间有关,2010-11-31发生了一些事情......
回答by Alexander Konstantinov
Neveruse math like 60*60*24*7 to add/subtract days (because of daylight time saving), use strtotime
or mktime
instead:
永远不要使用像 60*60*24*7 这样的数学来添加/减去天数(因为夏令时),使用strtotime
或mktime
代替:
$timestamp = strtotime('+7 days', $timestamp);
// Or something like this (it's OK to have day parameter <= 0 or > 31)
$timestamp = mktime(0, 0, 0, $month, $day + 7, $year);
Your example will be more obvious if you'll output time as well:
如果你也输出时间,你的例子会更明显:
$timestamp = mktime(0, 0, 0, 10, 28, 2010);
echo date('Y-m-d H:i:s', $timestamp) . "\n";
$timestamp += 60*60*24*7;
echo date('Y-m-d H:i:s', $timestamp) . "\n";
Output:
输出:
2010-10-28 00:00:00
2010-11-03 23:00:00
Here you have 2010-11-03 23:00:00
instead of 2010-11-04 00:00:00
because one of the days (31 Oct) is 25hours long instead of 24.
在这里你有2010-11-03 23:00:00
而不是2010-11-04 00:00:00
因为其中一天(10 月 31 日)是25小时而不是 24 小时。
回答by Artyom
It looks like you moved to daylight time savings or back from them.
看起来您转向夏令时或从他们回来。
POSIX time (timestamp) is always UTC while local time is UTC+X were X may change from date to date according to time savings.
POSIX 时间(时间戳)始终为 UTC,而本地时间为 UTC+X,根据节省的时间,X 可能会从日期更改为日期。
回答by Mario
It really seems to be time zone/DST related.
它似乎真的与时区/夏令时有关。
Adding
添加
date_default_timezone_set('UTC');
as the first line solves the issue (as you no longer use any DST settings).
因为第一行解决了问题(因为您不再使用任何 DST 设置)。
回答by Sol
In case someone needs this, just like me referencing the site: Today, current time, Y-m-d H:i:s, plus +7 Days. OR you could add +3 Months, etc.
如果有人需要这个,就像我引用该站点一样:今天,当前时间,Ymd H:i:s,加上 +7 天。或者您可以添加 +3 个月等。
$timestamp=strtotime("+7 Days");
$nowtime = date('Y-m-d H:i:s', $timestamp);
回答by Gordon
I will never understand why people use this horrible mktime
function.
我永远不会明白为什么人们使用这个可怕的mktime
功能。
In case you are on PHP5.3 yet, you might want to consider using
如果您使用的是 PHP5.3,您可能需要考虑使用
$period = new DatePeriod(
new DateTime('28-10-2010'),
DateInterval::createFromDateString('1 week'),
4);
foreach ( $period as $dt ) {
echo $dt->format( "l Y-m-d H:i:s\n" );
}
Without PHP5.3 you can still use
没有PHP5.3你仍然可以使用
for($i=0; $i<=4; $i++) {
echo date('Y-m-d', strtotime("+$i weeks 28-10-2010")), PHP_EOL;
}
DateTime questions are pretty common on SO, so you might find interesting cases when searching.
DateTime 问题在 SO上很常见,因此您在搜索.
回答by Evernoob
I believe this does have something to do with daylight savings time because the 'time' segment of your timestamp is set to midnight on the dot, but when daylight savings finishes we go back an hour - which is still the previous day (at 11pm).
我相信这确实与夏令时有关,因为时间戳的“时间”段设置为点上的午夜,但是当夏令时结束时,我们将返回一个小时 - 这仍然是前一天(晚上 11 点) .
If you add 2 hours to the original line so you have this:
如果您在原始行中添加 2 小时,那么您将拥有:
$timestamp = mktime( 2, 0, 0, 10, 01, 2010 );
and therefore start at 2am rather than midnight... it works.
因此从凌晨 2 点而不是午夜开始......它有效。
回答by DBQ
I have had a great deal of success with this methodology for adding days, though i have not tested with your format:
尽管我还没有使用您的格式进行测试,但我使用这种方法取得了很大的成功:
$date = The value of the database from your query
$daysahead = The number of days ahead you want to calculate
$final_date = date(“m/d/Y”, strtotime($date) + (86400 * $daysahead));
echo $final_date;