Вопрос

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

Это было полезно?

Решение

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
*/

Другие советы

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 :)

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top