6

I have a web scheduling app that I'm currently rewriting and have some questions about how to work with recurring appointments (I know there is no shortage of "what's the best way to do this" when it comes to recurring appts).

So I want to offer recurring appointments where the user can schedule an appointment for a date like Sat, June 2nd, and it should repeat every other week on Saturday for some pre-determined time period (like 1 year).

What php functions can help me determine which date "every other saturday" falls on? I'm attaching a picture of my UI for clarification.

enter image description here

Greg
  • 6,453
  • 9
  • 45
  • 61
  • 1
    Stuff like `strtotime('Next Saturday')` is easy enough, unless you want more complicated things :) – Ja͢ck Jun 02 '12 at 08:19
  • So is it being used in a calendar for the user, where they actively seek out the information about when their appointment is if they need a reminder? Or does it need to send them some automated message or something like that on its own? – Max Spencer Jun 02 '12 at 08:31
  • Hi Max, my app lets users schedule appointments and then sends reminders to their clients of the upcoming appointment. In this case, I want to accommodate for a user booking an appointment "every other week on saturday" (or any other combination that my UI can create). My concern was that just adding 14 days (in this case) wouldn't always mean the appointment would fall on a Saturday. – Greg Jun 02 '12 at 09:40

3 Answers3

19

Personally I'd look to work with DateTime objects and use the DateInterval class.

In your above case, you need to work out the date of the first/next saturday, then just work with a 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
*/
salathe
  • 51,324
  • 12
  • 104
  • 132
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • Thanks for adding the example salathe – Mark Baker Jun 02 '12 at 09:15
  • I think this is what I'm looking for! Thanks Mark. I'm gonna give this a try in the morning, the input is much appreciated! – Greg Jun 02 '12 at 09:41
  • Hi Mark, it looks like some of the classes your using are only available in php 5.3.x (DatePeriod, DateInterval). Unfortunatly I tried to upgrade my server this morning and it borked a bunch of joomla sites I'm running so I had to revert to 5.2.9. Is there a similar approach to your solution using some flavor of 5.2.9 functionality? – Greg Jun 03 '12 at 20:11
  • @Greg - Yes, I make the assumption that most developers should be using at least PHP 5.3.x as the 5.2 branch of PHP ceased to be supported in August last year... If you're stuck with 5.2.x you'll have to do things the more long winded way, though you should still have DateTime rather than needing to revert to strtotime – Mark Baker Jun 03 '12 at 20:59
0

Well, I would set a start date first; when that day has come you just do:

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

This gets you the same day, but just two weeks later. And the cycle continues :)

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
0

you can use a database column to store your time in seconds:

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

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

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

the id(s) selected are 1 week old or more.