tags:

views:

55

answers:

3

I've got two times that need to be stored into a database. The time format is hh:mm:ss with NO DATE. These times can be changed by the users on the system. One is a time on and the other is a time off. The time off should always be greater than the time on within a 24 hour cycle.

Here is the part that I'm having trouble with. I don't want to limit the user to selecting times before midnight to keep everything in the same "daily" cycle so I'd like to be able to logically determine if the users' times are simply within a 24 hour time period and then test that the on time is always less.

Can someone help me work through this? There are so many time and date functions that I really don't know which one(s) I need to do this; plus, I'm unclear on how I should test for this.


I'm starting to think that there is no way to test for this without having a date included. Just the times is not enough.

+1  A: 

Any two times in hh:mm:ss format are going to be within a 24 hour time period, as you state. So unless you actually store a date, I am not sure how you can do this.

If I understand correctly, a start time of 23:00:00 and an end time of 04:00:00 should be acceptable to you (this just means 5 hour work shift)? If this is acceptable, then can you give me an example of unacceptable input?

Perhaps you want to check that the end time is within 12 hours of the start time? That should be feasible.

Tarydon
Yes, exactly what I am looking for. From say, 23:00:00 hours to 04:00:00 would be 5 hours total and IS what I need. What is not acceptable would be 23:00:00 hours on and 04:00:00 off, the exact same scenario and PHP doesn't catch it. I was using Zombat's example above and if you run that, you'll see that it isn't correct even though the times are good.
Todd PA
Actually, I think it should be 23:59 hours from the start time. I think that is what I need. Thanks for helping me see this. :)
Todd PA
+2  A: 

The time is always within a 24 hour cycle, so if the user puts 01:00/03:00 he's on for 2 hours If he writes 03:00/01:00 he's on for 22 hours.

I dont see the problem.

gavit
It isn't working that way though. For some reason, using Zombat's example above, you will see that it doesn't work. I think its because PHP is formatting the date from 00:00:00 hours.
Todd PA
I also should add.. If the use puts in 23:00:00 hours for on and 03:00:00 for off, (strtotime($onTime) >= strtotime($offTime)) will not work even though it should.
Todd PA
@Todd - why bother comparing times at all if any on time and any off time forms a valid range? My answer only worked if you're comparing times on the same day, which doesn't appear to fit the requirements here.
zombat
+1  A: 

The OP wrote in a comment:

The user can opt to get a report delivered in a window of time. The user may opt to have their reports delivered in a window from 23:00:00 to 01:00:00 hours. They may decide tomorrow that that time is no longer good and change it to 23:0:00 to 05:00:00 or something like that. Am I missing something??

You have no problem in the time definition part. You may want to play with the code that sends out the report.

// current time
$timeNow = time();

// fetch user time options from database
$timeOn = [from the database];
$timeOff = [from the database];

// convert times to seconds from epoch
$timeOn = strtotime($timeOn);
$timeOff = strtotime($timeOff);

// if database time is in timestamp format,
// only the hour, minutes and second information is needed
$timeOn = mktime(date("H", $timeOn), date("i", $timeOn), date("s", $timeOn));
$timeOff = mktime(date("H", $timeOff), date("i", $timeOff), date("s", $timeOff));

// if time on is higher than time off, time on is of yesterday
if($timeOn > $timeOff){
    $timeOn = strtotime("-24 hour", $timeOn);
}

// decide on report sending
if($timeNow >= $timeOn && $timeNow <= $timeOff){
    // Send report
} else {
    // Do not send report or reschedule the report
}
Nirmal
Nirmal your beautiful! That works bro. Thank you for this help!
Todd PA
I had everything you had but I was missing : if($timeOn > $timeOff){ $timeOn = strtotime("-24 hour", $timeOn);}. This was what I was missing. Thank you again Nirmal.
Todd PA
Glad that I'm helpful. Your question misled every participant here. You should have asked on the reporting part instead of the user input part. Anyway, good luck!
Nirmal
The funny thing about this is that this piece of code that I'm working on isn't for the actual sending of the reports but for the form validation itself. I originally thought it would be better and cleaner to use a trigger for this but wasn't able to find anyone that could help. Triggers aren't my strong point.
Todd PA
Again, why do you want to validate the user input in the first place? If any time is going to be OK, what you want to validate?
Nirmal
Well, I need to validate the input because the database query gathers all of the reports based on these two times. Mysql also doesn't recognize times that are technically into the next day so I needed to make sure that before the user saves their preferences that the times were correct or else they wouldn't get any reports. Do you see a better way of doing this? I'm still open to other suggestions.
Todd PA
You can just save the times directly without checking for which day they are for. Because, whatever the numbers are, they are going to be valid for a time window case. If 2:00 pm of today is not valid, it's 2:00 pm of yesterday. And this logic will be handled during the report sending part. That's all. Or are you trying to do something else?
Nirmal
You know Nirmal, I think you may be onto something here... I do know this.. My query will not return any results if the off time is after midnight (00:00:00). So your saying that I should just forget about this PHP code and redo the database code? How would tell MySQL to back the date 24 hours like you did in PHP?
Todd PA
Unless I clearly understand your application logic, I can't comment on that any more. Wish you good luck.
Nirmal
Thanks Nirmal. I appreciate everything.
Todd PA