Hi,
I want to loop through and print the following to screen:
Example:
Sun, Aug 11 (this is the nearest Sunday to to now on the server in the future)
Sun, Aug 18
Sun, Aug 25
Sun, Sep 1
The critiera are:
Nearest Sunday to now on the sever
Does not include Sundays in the past
Prints ten times (i.e. 10 weeks forward from today – starting on the Sunday today or forward)
Note that as time moves forward, of course the nearest Sunday to now will as well.
I know how to do for loops, but am stymied by how to print out the first day and then increment using some form of PHP DateTime function.
Any suggestions much appreciated!
An approach might look like this:
<?php (for $i = 0; $i < 10; $i+=7) { echo $date } ?>
Where $date is the nearest Sunday to now in the future or today.
Thanks
karen
Have you checked the PHP Online Manual for the date() function?
It is quite involved but I think you should first test for the numeric day of the week or the actual long or short day of the week which is N, l, D respectively.
Increment a 0…6 for next-loop and break out (with the relevant date) once Sunday is encountered.
Proceed with the for-next loop for the next 10 weeks.
chorn
August 12, 2019, 6:18am
3
the DateTime
class can handle values like you would speak them, e.g.
print_r((new DateTime)->modify('next sunday'));
then you can just use the modify()
function within a loop with '+1 week'
, or use a combination of DateInterval and DatePeriod.
Hi
I found the fact that I could number days incredibly useful. I can think of at least 3 ways I can use that elsewhere.
In the meantime, the simplest version was as follows:
My final version is as folows:
<?php for ($i = 0 ; $i < 10; $i++) : ?>
<?= date('l - d/m/Y', strtotime('this week + '.$i.'week'));?>
<?php endfor; ?>
Thanks for all the helpful suggestions. They led me down the right road
Also I went with Monday, but for Sunday start a solution may be found here:
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
system
Closed
November 12, 2019, 8:09pm
5
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.