using php and strtotime to produce a list of reoccurring events for the next 7 days without using a database?

Tags: php strtotime
By : BBLJ84
Source: Stackoverflow.com
Question!

Scenario

I have 3 clubs each hold several classes on certain days i.e

  • Club 1, Class Friday 6-7pm, Class Friday 7-8pm, Class Friday 8-9pm
  • Club 2, Class Tuesday 7-8pm, Class Tuesday 8-9.30pm
  • Club 3, Class Monday 7-8.30pm, Class Wednesday 7-8.30pm, Class Friday 7-8.30pm

and i want to run a script to return a list of classes for the coming week. but i am unsure of the best approach.

Ideally i don't want to use a database as each week the classes are held on the same night at the same time.

So what i think i want to achieve is:

  • get the current day
  • get the next 7 days from current day and their date
  • run a check on each day of the upcoming week to see if there is a class on
  • list the classes if matched

it sounds easy in principle but these things never are, for me at least.

what i have so far for the current day is

$today = strtotime('today 00:00:00');

and for the next 7 days i have

$this_week_start = $today;
$this_week_end = strtotime('+7 today 23:59:59');

am i on the right tracks? What I'm unsure how to do is use the above to cycle through the next 7 days from $today and check for events?

Hope this makes sense

Would appreciate any help

By : BBLJ84


Answers

Sometimes, you just can't use a database for whatever reason. So here's my attempt at this problem you have.
First, I decided to sort the the data you have into something which uses as less deep array levels as possible.
So this:

  • Club 1, Class Friday 6-7pm, Class Friday 7-8pm, Class Friday 8-9pm
  • Club 2, Class Tuesday 7-8pm, Class Tuesday 8-9.30pm
  • Club 3, Class Monday 7-8.30pm, Class Wednesday 7-8.30pm, Class Friday 7-8.30pm

    Turned into:

  • Monday -> Class37830
  • Tuesday -> Class27800, Class28930
  • Wednesday-> Class37830
  • Friday -> Class16700, Class17800, Class18900

    I don't know what you wish to do with the names, I suspect change them to the actual name. So I wanted to make each class unique and added a code.
    Take Monday's only class, "Class37830" for example.
    3 = Club #
    7 = Start hour
    830 = Finish H:mm Using substr() you can find this info and display it if you wanted

    SO isn't letting me post the code, so I've moved it to Pastebin

    Output of this is (for today, a Wednesday) is :

    Class for Friday

  • Class16700
  • Class17800
  • Class18900
    Class for Tuesday
  • Class27800
  • Class28930
    Class for Wednesday
  • Class37830



  • I believe that, alternatively to the suggestions you already recieved, you might want to try the following. Using logicals is probably one of the best options for such a problem, since you don't need to use for-loops:

    function out = clear_matr(A)
        % ind is true for all the rows of A, where the highest value is not equal to 1
        ind = ~(max(A, [], 2) == 1);
    
        % filter A accordingly
        out = A(ind, :);
    end
    
    By : merch


    Subtract the julian day integer of the earlier date from the julian integer of the later date. The following tells you exactly how to do that.

    http://www.cs.utsa.edu/~cs1063/projects/Spring2011/Project1/jdn-explanation.html

    Otherwise, http://pdc.ro.nu/jd-code.html already has a C version

    long gregorian_calendar_to_jd(int y, int m, int d)
    {
        y+=8000;
        if(m<3) { y--; m+=12; }
        return (y*365) +(y/4) -(y/100) +(y/400) -1200820
              +(m*153+3)/5-92
              +d-1;
    }
    


    This video can help you solving your question :)
    By: admin