calender esque question...

I have a while loop generating and inserting data into a mysql db, what I’m having trouble figuring out is how to accurately generate a date based on $i… for example today is Sunday the 15th of April… the loop will run until $i(0) reaches 24, how can I generate the date(YYYY-MM–DD) for Sunday for the corresponding week that $i represents?

Thanks for any help!

so I created a loop that grabs the current day and compares it to the number of days in the current month, if $i is 0, then once $i goes up we multiply it by the 7 days and add it to the current date, then throw it into a loop comparing it to the number of days in the month if it exceeds it changes the month/year and grabs the days in that month, only to try again until we have an acceptable day. It works but only for the current and 1 month after then doesn’t. I’ll post the code when I get a chance but its seems like its too much to do something that I didn’t think should be so complicated, and I’m not that familiar with mktime() so I’m wondering if it can be done that way more efficiently rather than having multiple loops inside of loops inside of loops?

Fairly simple… Just use the date and strtotime functions. You can use them to create any type of calendar info such as … Third Monday of the month, first day of the month, second saturday in may, just about anything.

Here is a sample for 5 days from current date…
echo “5 days from today is: " . date(“Y-M-d”, strtotime(”+ 5 days " . date(“Y-M-d”)));

3rd Monday in THIS month:
echo "Third Monday of this month is: " date(“Y-M-d”, strtotime("third monday " . date(“Y-m-t”, strtotime(“last month”))));

So, to use this format, you would just include your count variable to alter so many days…
Something like this: (Not tested…)
echo “5 days from today is: " . date(“Y-M-d”, strtotime(”+ " . $i . " days " . date(“Y-M-d”)));

That should work for you… Good luck…

Ahh, I was trying to find this thread earlier. :slight_smile: thanks I knew there had to be an easier way to do this than I was attempting.

Well, actually there are many other ways to use these functions… Here is a link to them. You might find some of them useful in the future… Glad I could help!

http://php.net/manual/en/datetime.formats.relative.php

yeah I’ve looked at it and while I haven’t spent much time on it, every time I’ve gone to learn it it just seems too confusing for the time that I have available… one of these days I’ll sit down on it. :slight_smile: But before this I had it with 24+/- lines and loops and this is just going to be much more efficient. thanks again

Glad I could help… Come back with your next puzzle… We love to solve them… LOL…

Sponsor our Newsletter | Privacy Policy | Terms of Service