Jump to content

[SOLVED] Time slots: Help with math/logic rather than code.


zq29

Recommended Posts

Hello guys and girls,

 

I'm looking for a bit of help with some math / logic more than anything. So here's a bit of background on what I'm doing... I'm writing a small booking system where the user can select a start/end date and time, although the time slots are predefined by radio buttons.

 

There are three start and end time options, for example - Start: 09:30, 12:30, 19:30; End: 11:30, 14:30, 23:30.

 

Now I'm trying to work out how many 'time slots' a user has selected, a single slot being 09:30 - 11:30, two slots being 09:30 - 14:30, and so on.

 

I started out converting the date/time string to a unix timestamp then subtracting one from the other and dividing by 86400 to work out how many whole days had been selected and divided by three to get the amount of slots selected, this started getting messy quite quickly and doesn't really work when selecting start and end dates mid-date.

 

Any ideas on how I can go about this? Feel free to demonstrate your theory with pseudo code, or PHP code if it's easier. I'm crap at maths and this kinda thing takes me hours to figure out - Am I even on the right tracks so far?

 

Many thanks in advance.

Link to comment
Share on other sites

No, say I select 15/02/2007 09:30 as the start time and 17/02/2007 14:30 as the end time, I need my script to work out that the user wishes to book 8 time slots, so I need the math/process/function to return 8 from the inputs 15/02/2007 09:30 and 17/02/2007 14:30

Link to comment
Share on other sites

Ok, I'm confused between your first and last post.

 

I need some clarification.  If you have x number of start times, is each of those start times indicating the beginning of 1 time slot?  Or can you have more than one time slot per start/stop time?

 

If it's the first, and you're storing them in a DB:

 

slotid | starttime | stoptime | etc

 

Then you just run some SQL:

SELECT COUNT(slotid) FROM slots WHERE starttime >= starttime_selected AND stoptime <= stoptime_selected

 

If it's the second and your calculating slots between points A and B, it gets a little more tricky.  So let me know if you need that worked out or explain what's wrong from my understanding above.

Link to comment
Share on other sites

If it's the second and your calculating slots between points A and B, it gets a little more tricky.  So let me know if you need that worked out or explain what's wrong from my understanding above.

Yes, I'm trying to calculate the number of slots selected between date/time A and B. Apologies for the confusion.

Link to comment
Share on other sites

Alright... well that introduces a bunch of other problems.

 

1) Are we working with an 8 hour day or are these time slots on a 24 hour basis?

2) Are the time slots simply split up evenly between the two points or are they allotted based on a certain number of hours?

3) If they are back to back, is there a break in between?

 

Details man, details.

Link to comment
Share on other sites

I've just figured it out, now this probably isn't the most efficient way to go about this, but it works - Reading my code will probably explain my initial problem a bit better. Thanks for sticking around though ober :)

 

<?php
function return_slots($start,$end) {
    $c  = 0;
    $s  = strtotime($start);
    $e  = strtotime($end);
    $et = array("11:30","14:30","23:30");
    for($t=$s; $t<=$e; $t+=3600) if(in_array(date("H:i",$t),$et)) $c++;
    return $c." slots";
}

echo return_slots("2007-02-15 12:30:00","2007-02-17 11:30:00");
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.