/    Sign up×
Community /Pin to ProfileBookmark

Add a day to a given date

AAAARRRHHH!! 😡 😡

This has been bugging me for ages:

I have a date – $date = “20050922”

What i would like to do is echo the date ($date) until it = date(‘Ymd’)

eg.

[COLOR=Red]While ($date <= date(‘Ymd’)){
//Here add one day!
echo $date;
}[/COLOR]

I have tried many ways, but i cannot get it to work.

Help will be very greatfully recieved.

Thank you in advance

Alex

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYSep 27.2005 — why dont you handle that with the timestamp of that date, which is the number of seconds from 1970, you can get the timestamp from your date using the mktime() function, then add 86400 to the date until its the date you want
Copy linkTweet thisAlerts:
@LimpBagelSep 27.2005 — If you are storing them as unix timestamps just add 86400 (the number of seconds in a day).

Then compare them in the while loop like this:

while(time() - $date > 86400)

That will tell you if you are within 24 hours of each other. But if want it to be the same date do:

while(date("Ymd", $date) != date("Ymd"))

Just put a check in so that you don't put in a date that is after today, that last one would be an inf loop in that case.
Copy linkTweet thisAlerts:
@NogDogSep 27.2005 — [code=php]
<?php
$date = "20050922";
$timestamp = strtotime($date);
while($timestamp <= time())
{
echo "<p>" . date('Ymd', $timestamp) . "</p>n";
$timestamp += 60*60*24;
}
?>
[/code]
Copy linkTweet thisAlerts:
@alexthomasauthorSep 27.2005 — Sweet!

Cheers Limpbagel, your idea worked a treat!!! ? ? ? ?
×

Success!

Help @alexthomas 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.20,
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,
)...