[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 787
  • Last Modified:

PHP: Generate List of dates

I am creating a website that changes its content every tuesday. Does anyone know how to create a list of dates that are Tuesdays, by specifying a start and end date. I require the format to by yyyy-mm-dd.

Thanks for your time.
0
markgales
Asked:
markgales
1 Solution
 
Austin_HastingsCommented:
The PHP date() function will take a format string and a timestamp.

Use the "w" format code to determine todays day-in-week index (Tuesday will be a 2). Use some simple arithmetic to decide how many days from today until next tuesday ((2+7 - n)%7).

Take that number, multiply it by 24 * 60 * 60 (secs/day). Add this many seconds to your timestamp. The new timestamp is a tuesday.

From this point forward (or backward) just add 7 * 24 * 60 * 60 to your timestamp and print the date out however you want using the format string in date().

For your yyyy-mm-dd format, that's going to be: date("Y-m-d", $timestamp)



0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now