I'm looking to create a script to count how many hours/mins a contractor has worked within his standby times.
Standby times show a contractors default standby times.
The script needs to count how many minutes the contractor has worked within these hours. It should also be compatible if the work rolls into a new day (see example below).
Standby Times
Standby Time From (Example: 18:00)
Standby Time To (Example: 06:00)
Travel Start Time
19.30
Site Arrival Time
20.30
Home Time
03:30
The above example should return 8 hours 0 mins STANDBY. I have explained why below.
The contractor travels to work at 19.30.
The contractor arrives on-site at 20.30.
The contractor arrives at home at 03:30.
This means the contractor has spent the hours 19.30 - 03:30 (8hrs) on STANDBY.
Any help appreciated :)
EDIT
I originally was using this script, but it doesn't work when the hours worked rolls into two days.
// get travel start timestring
$travel_time = 70200; //19:30 in seconds
// get home time in seconds
$home_time = 12600; //03:30 in seconds
// get standby_from and standby_to in seconds
$standby_from = 64800; // 18:00 in seconds
$standby_to = 21600; // 06:00 in seconds
// check how many seconds we worked during standby hours
for($sec_of_day=$travel_time;$sec_of_day<=$home_time;$sec_of_day++)
{
if($sec_of_day >= $standby_from && $sec_of_day <= $standby_to)
{
$seconds_sb++;
}
}
echo $seconds_sb; // returns how many seconds
$to_time = strtotime("2008-12-13 10:42:00");
$from_time = strtotime("2008-12-13 10:21:00");
echo round(abs($to_time - $from_time) / 60,2). " minute";
A simple exemple, you have to convert your date in secondes with strtotime(), and make the difference between the two timestamps.
Related
i have time shifts which are assigned to the user. Suppose a night shift starting time is 21-00-00 pm of one july and its ending time is 03-00-00 am of 2nd July. Now i want to get total time a employee worked by adding start time to end time which is equal to 6 hours and i should get six hours. I have tried following code which is working fine for current date like it will give me exact 6 hours if start time is equal to 15-00-00 pm of 1 july to 21-00-00 pm of 1 july but it will fail when shifts exists between two dates as i mentioned above.
$attendance_start_time = \Carbon\Carbon::parse($shift->start_time);
$attendance_end_time = \Carbon\Carbon::parse($shift->end_time);
$total_attendance_time=$attendance_end_time->diffInSeconds($attendance_start_time,true);
\Carbon\CarbonInterval::seconds($total_attendance_time)->cascade()->forHumans()
i am expecting six hours but it is giving me following result
18 hours
i want exact six hours
Not sure if it will fully solve your problem, but check out this :
\Carbon\CarbonInterval::seconds(4100)->cascade()->forHumans(['aUnit' => true]);
UPD:
It might be this solution will work in your case, but make sure that you have tested all of the edge-cases:
$startTime = \Carbon\Carbon::parse('2022-07-02 19:00');
$endTime = \Carbon\Carbon::parse('2022-07-02 19:30');
$diff = $startTime->diffInSeconds($endTime);
if ($endTime->greaterThanOrEqualTo($endTime) && ! $endTime->isSameDay($startTime)) {
$diff = $startTime->diffInSeconds($endTime->addDay());
}
$humanReadable = \Carbon\CarbonInterval::seconds($diff)->cascade()->forHumans(['aUnit' => true]);
I want to display content from the database with dates up to 2hours ahead of time.
Example:
2018-11-09 20:00:00.000000
2018-11-08 19:00:00.000000
2018-11-06 19:00:00.000000
2018-11-06 18:00:00.000000
Lets say the time and date is
Nov 6th at 6pm. I want the bottom two entries to be displayed and the two future dates to not show until the current time is within 2hours of that time.
My code is as follows:
$cT = strtotime($row3['MissionTime']) - strtotime("now");
if($cT <= strtotime('-2 hours')) {
echo $row3['MissionTime']."<br>";
}
I've tried several different ways but I can't seem to get this to work right. Help and tips?
The reason your code doesn't work is that strtotime returns a number of seconds since the unix epoch. When you subtract two results of strtotime you will get a number of seconds difference which is as you expect. However you cannot compare that value to strtotime('-2 hours') as the output of that will be the timestamp for 2 hours before now (which right now is 1541539906), so the test will always pass. You should just compare it to 7200 instead (I'm pretty sure based on your question description that +7200 is more appropriate than -7200). so change
if($cT <= strtotime('-2 hours')) {
to
if($cT <= 7200) {
Note that it is almost certainly better to do this in your query. Try adding a condition on your time column as something like
WHERE MissionTime <= NOW() + INTERVAL 2 HOUR
And then you won't need to check in the PHP at all.
strtotime() returns a timestamp in seconds. Subtracting two timestamps gives you a difference between those two timestamps, in seconds.
So if strtotime($row3['MissionTime']) is a timestamp that's 1.5 hours in the future, and you subtract strtotime("now") from it, you end up with a difference of 5400 seconds (60 seconds * 60 minutes * 1.5 hours).
strtotime('-2 hours') gives you the timestamp for 2 hours ago, which is currently somewhere around 1.5 billion. This is not very useful for your situation.
Here are two ways to modify your code:
$cT = strtotime($row3['MissionTime']) - strtotime("now");
if($cT <= 7200) {
echo $row3['MissionTime']."<br>";
}
If the difference between $row['MissionTime'] and now is less than 7200 seconds (60 seconds * 60 minutes * two hours), $row3['MissionTime'] is either in the past or it's within the next two hours.
Alternatively:
if(strtotime($row3['MissionTime']) <= strtotime('+2 hours')) {
echo $row3['MissionTime']."<br>";
}
Basically the same, but perhaps more readable if you're not planning to use $cT for anything else. This simply checks if $row3['MissionTime'] is earlier than whatever time it will be in +2 hours.
Time is confusing... How can one calculate the time elapsed from PM to AM in php?
For example 22:00:00 (10pm) to 02:00:00 (02am) should give 04 hours elapsed. Instead my code returns -08 hours
function hours_elapsed()
{
$timestart = strtotime("22:00:00");
$timestop = strtotime("02:00:00");
$time_diff = $timestop - $timestart; //time difference
return gmdate("h", $total_hours);
}
It's clear that the code calculates in 24 hour format so of course it returns -08 but how is it possible to get time elapsed without 24 hour constraint.. when it passes the midnight mark?
-8 hours is 4 hours. Just add 12 if the number is negative:
$time_diff = $timestop - $timestart; //time difference
if ($time_diff < 0) {
$time_diff += 12;
}
This won't help you, however, if your dates are more than one day apart. You need to specify the date as well (as in the day of the month) to tell PHP that those times are different.
Clearly 22:00:00 is 24 hour clock, 02:00:00 could be either though couldn't it?
add the date as well, or test for the negtive and add 12 hours.
Quick solution from me: add a day to the later date.
echo gmdate('h', strtotime('1970-00-01 02:00:00') - strtotime('1970-00-00 22:00:00'));
But Blender's answer seems to be easier. :)
I need to get average sleep interval from following data:
22:00-06:00
00:00-08:00
02:00-10:00
=> expected: 00:00-08:00
22:00-06:00
00:00-08:00
02:00-10:00
04:00-08:00
=> expected: 01:00-08:00
The problem is oscillation around midnight, where part is under 00:00 and part over 00:00.
Simple mean 22+00+02+04 doesn't work. I could count number of times over midnight (3 in this case) and if it's more than those before midnight, I should add 25 co compensate. But this doesn't count with those people, who work at night and go sleep around 8-14!
My theory is: First, somehow I need found peak, something like the most frequented area (e.g., in 9-10 there is 5 record, in 10-11, there is 3 etc.) and then I can decide co compensate border values by adding 24 hours.
What do you think?
What about taking into account relative difference with midnight ?
The result would be (-2+0+2+4)/4 = 00:45
Making the assumption that the person is not sleeping more than 24 hours, then make a method to calculate like so (pseudo code):
calculateSleepTime(sleepTime, wakeupTime) {
if (sleepTime > wakeupTime) {
return (24 - sleepTime) + wakeupTime;
} else {
return wakeupTime - sleepTime;
}
}
averageSleepTime(sleepTimesArray) {
totalSleptTime = 0;
totalTimesSlept = 0;
foreach (oneSleepTime in sleepTimesArray) {
totalTimesSlept++;
totalSleptTime += calculateSleepTime(oneSleepTime);
}
return totalSleptTime / totalTimesSlept;
}
After you get the average sleep time, calculate either the average sleep time, or average wake up time, and do addition/substraction to find your interval. The alternative to this is finding the average sleep time and the average wakeup time (taking relative to midnight times into account).
Determine a lower limit where you can say
Okay, people won't go to sleep this time of the day, if I get this time, it must mean they worked all night and only sleep now
and if the time is below that limit, add 24 hours to both start and finish.
Does anyone have an idea how to make a countdown ticker that shows the hours and mins left until a perticular time of day and on a weekend (Sat and Sun) the time left until 16:30 on Monday. It would need to reset # 16:30 everyday except the weekend days.
This has got me stumpped and really could do with some pointers.
Thanks,
B.
If you want the page to count down every second, rather than only on refresh, you should use JavaScript. There are many examples out there, and others can be found using google by searching for Javascript Countdown
If you wanted to do it in PHP, the best way is to use mktime() to get the unix timestamp of when the time ends, and the value from time().
Find the difference, and then you can calculate the time left:
$diff = mktime(...) - time();
$days = floor($diff/60/60/24);
$hours = floor(($diff - $days*60*60*24)/60/60);
etc.
EDIT
Javascript...
Basic idea of the date object
var date = new Date(); //gets now
var weekday = date.getDay(); //gets the current day
//0 is Sunday. 6 is Saturday
if ( weekday == 0 ){
date.setTime()(date.getTime()+60*60*24); //increase time by a day
}
if ( weekday == 6 ){
date.setTime()(date.getTime()+60*60*24*2); //increase time by two days
}
//need to check if we have already passed 16:30,
if ( ( date.getHours() == 16 && date.getMinutes() > 30 ) || date.getHours() > 16){
//if we have, increase the day
date.setTime()(date.getTime()+60*60*24)
}
date.setHours(16);
date.setMinutes(30);
//now the date is the time we want to count down to, which we can use with a jquery plugin
Would be JQUery also a solution? Because there are several plugins avalaible which are showing a countdown. With some additional code you could easily calculate the time till the next monday, 4:30 pm.