Adding days to a timestamp
Giving a starting date, I'm adding four times seven days to get 5 different dates separated exactly a week each one.
//$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 "Da开发者_如何学JAVAte=".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
$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?
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...
Never use math like 60*60*24*7 to add/subtract days (because of daylight time saving), use strtotime
or mktime
instead:
$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 25 hours long instead of 24.
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.
$timestamp=strtotime("+7 Days");
$nowtime = date('Y-m-d H:i:s', $timestamp);
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.
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).
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).
If you add 2 hours to the original line so you have this:
$timestamp = mktime( 2, 0, 0, 10, 01, 2010 );
and therefore start at 2am rather than midnight... it works.
I will never understand why people use this horrible mktime
function.
In case you are on PHP5.3 yet, you might want to consider using
$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
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.
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;
精彩评论