php - working with dates like "every other week on tuesday"
Asked Answered
S

3

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

Secure answered 2/6, 2012 at 7:57 Comment(3)
Stuff like strtotime('Next Saturday') is easy enough, unless you want more complicated things :)Baudekin
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?Omeara
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.Secure
T
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
*/
Transversal answered 2/6, 2012 at 8:57 Comment(4)
Thanks for adding the example salatheTransversal
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!Secure
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?Secure
@Secure - 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 strtotimeTransversal
O
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 :)

Outnumber answered 2/6, 2012 at 8:28 Comment(0)
J
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.

Johnnajohnnie answered 2/6, 2012 at 9:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.