Php - work with dates like "every second week on Tuesday"

I have a web planning application that I am rewriting right now, and I have some questions about how to work with recurring appointments (I know that there is no shortage of โ€œwhat is the best way to do thisโ€ when it comes to repetitive appt).

So, I want to offer recurring appointments when a user can schedule an appointment for a date, such as Sat, June 2 , and she should repeat every second week on Saturday for a predetermined period of time (like 1 year).

What php functions can help me determine what date is "every saturday"? I am adding an image of my user interface for clarification.

enter image description here

+6
source share
3 answers

Personally, I would like to work with DateTime objects and use DateInterval .

In the above case, you need to determine the date of the first / next Saturday, and then just work with the P2W date interval

Basic example

$dow = 'saturday'; $step = 2; $unit = 'W'; $start = new DateTime('2012-06-02'); $end = clone $start; $start->modify($dow); // Move to first occurence $end->add(new DateInterval('P1Y')); // Move to 1 year from start $interval = new DateInterval("P{$step}{$unit}"); $period = new DatePeriod($start, $interval, $end); foreach ($period as $date) { echo $date->format('D, d M Y'), PHP_EOL; } /* Sat, 02 Jun 2012 Sat, 16 Jun 2012 Sat, 30 Jun 2012 Sat, 14 Jul 2012 โ€ฆ Sat, 20 Apr 2013 Sat, 04 May 2013 Sat, 18 May 2013 Sat, 01 Jun 2013 */ 
+14
source

Well, first I would set a start date; when this day has arrived, you simply do:

 $next_date = strtotime('+2 weeks'); 

This will give you the same day, but in just two weeks. And the cycle continues :)

0
source

you can use the database column to store the time in seconds:

 UPDATE appointments SET dtime=UNIX_TIMESTAMP() WHERE id=5 

then you can check this value wherever a specific period has just arrived / passed:

 mysql_query("SELECT id FROM appointments WHERE dtime>='".(strtotime("-1 week"))."'); 

selected id - 1 week or more.

0
source

Source: https://habr.com/ru/post/917235/


All Articles