开发者

How to ensure that one time is always less than another

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 d开发者_Go百科on'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.


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.


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
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜