Date Time Assistance

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:

  1. Nearest Sunday to now on the sever
  2. Does not include Sundays in the past
  3. Prints ten times (i.e. 10 weeks forward from today – starting on the Sunday today or forward)
  4. 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.

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:

Thanks for all the helpful suggestions. They led me down the right road :slight_smile:

Also I went with Monday, but for Sunday start a solution may be found here:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.