How to know actual working shift with php

问题内容:

Hi I have a problem with php. I need to identify the current work shift from 3 options (24 hours format)

First shitf 06:00 to 13:59
Second shitf 14:00 to 21:59
Third shitf 22:00 to 05:59

I tried this but its not working as expected

$hour = date("0500");
$shift;
if ($hour >= 0600 && $hour <= 1359 ) {
$shift = 1;
}else if($hour >= 14 && $hour <= 2159 )
{
 $shift = 2;
}else
{
 $shift = 3;
}

Thanks in advanced

问题评论:

    
What are you getting as a result?
    
your days have 1359 hours?
    
shift Id from a mysql table
– Jiga Garcia
54 mins ago
1  
checkout the DataTime class. That is built for such tasks and makes it much easier to compare dates/times.
    
I shearched for an example here and found things like this :”1359″ “2222” etc
– Jiga Garcia
53 mins ago

答案:

答案1:

Maybe:

$hour = data('H');
if($hour >= 6 && $hour < 14) {
    $shift = 1;
} else if($hour >= 14 && $hour < 22) {
    $shift = 2;
} else {
    $shift = 3;
}

答案评论:

答案2:

You could try something like this perhaps:

$hour = data('H');

switch( true ){
    case ( $hour >= 6 && $hours < 14 ):$shift=1; break;
    case ( $hour >=14 && $hour < 22 ):$shift=2; break;
    default: $shift=3; break;
}

答案评论:

    
How can I add seconds to the hour variable to be 24 hour format and compare that in your code
– Jiga Garcia
37 mins ago
    
do you really need seconds? if the current time was for example 13.59.59 then the int value 13 is still less than 14, but when the clock ticks over and the time is 14.00.00 the condition is no longer valid so I’m not sure you need the second as such
    
sorry im not that good at this but it you think so Its fine for me you seem to have more experience than me
– Jiga Garcia
19 mins ago
    
but I would like to have minutes if thats ok or could work for you
– Jiga Garcia
19 mins ago

答案3:

Put your time in quotes otherwise starting with 0 makes it an octal number


Also stick with one format if you are going to be comparing

$hour = date("Hi", strtotime("05:00"));
$shift;
if ($hour >= "0600" && $hour <= "1359" ) {
    $shift = 1;
}else if($hour >= "1400" && $hour <= "2159" ) {
    $shift = 2;
}else {
    $shift = 3;
}

答案评论:

    
I put 0500 as example to show my code its not working but the $hour variable should be equal to (Hours minutes and if possible seconds)
– Jiga Garcia
38 mins ago
    
oh then i didnt need to comment out the date function. you can use it and pass in any acceptable datetime format
    
valid time formats include 0530, 05:30, 05.30.12, etc

原文地址:

https://stackoverflow.com/questions/47755762/how-to-know-actual-working-shift-with-php

Tags:

Add a Comment