/    Sign up×
Community /Pin to ProfileBookmark

[CODE]<?php
$today = date(“j-n-Y”);

$datetime_temp = strtotime($today);
$datetime_end = strtotime(“30-11-2009”);

while($datetime_temp <= $datetime_end) {
echo date(“j-M-Y”, $datetime_temp).”<br />”;
$datetime_temp += 86400; //there are 86,400 seconds in a day
}
?>[/CODE]

The script above should print out all the dates between today and 30-11-2009. Except that the last day that gets printed out is
29-Nov-2009. But if I change $datetime_end to: $datetime_end = strtotime(“30-9-2009”);
The last day to be printed out is 30-Sep-2009.
I don’t understand what the problem is, if I increase the date it omits one day, but if the date is lower it prints out the final day. What is the problem?

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@MindzaiAug 19.2009 — Probably to do with the time of day at which you run the script since you are not accounting for time. Try this:

[code=php]
$from = mktime(0, 0, 0, date('m'), date('d'), date('y'));
$to = mktime(23, 59, 59, 11, 30, 2009);

for ($from; $from <= $to; $from += 86400) {
echo date("j-M-Y", $from) . "<br />";
}
[/code]
Copy linkTweet thisAlerts:
@SodbusterAug 19.2009 — If you read the dates your script prints out, you'll see that Nov. 1st is listed twice. There are more than 86400 seconds in that day since that's when daylight saving time ends (in the U.S.). Try this:[code=php]$datetime_start = strtotime('today');
$datetime_end = strtotime('30-11-2009');

$datetime_temp = $datetime_start;
while ($datetime_temp <= $datetime_end) {
echo date('j-M-Y', $datetime_temp) . '<br />';
$datetime_temp = strtotime('+1 day', $datetime_temp);
}[/code]
×

Success!

Help @happyharry spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...