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.
Related
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.
I need a php code that when you type (time), and a (base number), it increases by base number value, on specific time intervals.
e.g:
every 40 minutes, add 0.0001 to the base number 0.04
0.04
0.0401
0.0402
.....
You can use the unixtime and calculate how many 40 minutes that has elapsed.
$start = 1537088883; //Unix time of when I started writing answer.
$elapsed = time() - $start; // elapsed seconds since start
$counts = floor($elapsed/(60*40)); // number of 40 minutes that has elapsed
echo 0.04 + (0.0001*$counts);
https://3v4l.org/q9Vhm
Right now it will output just 0.04, but if we manipulate the $start we will get a different output ( or just wait 40 minutes and the same code will output a different number).
https://3v4l.org/E9XWN
If you want to set a start date and time you can use strtotime to convert a human readable date to Unix time.
$start = strtotime("2018-09-01 09:00:00");
https://3v4l.org/c6g0l
I am currently getting the difference between two times in minutes like this below, and it works fine...
$time1 = strtotime($firsttime);
$time2 = strtotime($secondtime);
$interval = abs($time2 - $time1);
$diffinminutes = round($interval / 60);
But say for example...
$firsttime = 02:05
$secondtime = 20:05
This calculation would return 1080 minutes (18 hours x 60 minutes).
I would like to be able to get the shortest difference, based on the time being a connected circle (i.e. like a clock haha).
So in the example above, I would rather it "go backwards" from 02:05 until it hit 20:05 and thus return 360 minutes (6 hours x 60 minutes).
In other words, I'm looking to get the shortest difference, regardless of which direction we go to find it.
Hopefully this makes sense.
Any insight on how to accomplish this would be appreciated, thank you!
(This assumes your inputs are just times and not timestamps.)
If your result is greater than 12 hours, then just subtract it from 24:
if ($result > 720) {
$result = 1440 - $result;
}
You might want to change the > to >= depending on which side you want ties to fall on.
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.
I want to put a php daemon to sleep (with System_Daemon::iterate())
so it runs max 20 times randomly spread over an hour. maybe a min distance would be smart so it doesn't run 20 times in the first half hour and 0 times in the second half.
i'm kinda stuck here and don't know how to start with this one, any help is very apreciated!
You may use cron jobs, to set the script to run ever so often.
http://net.tutsplus.com/tutorials/php/managing-cron-jobs-with-php-2/
... Crontab:
0 9 * * * /path/to/bashscript
and in /path/to/bashscript:
#!/bin/bash
maxdelay=$((1*60)) # every hour, converted to minutes
for ((i=1; i<=20; i++)); do
delay=$(($RANDOM%maxdelay)) # pick an independent random delay, 20 times
(sleep $((delay*60)); /path/to/phpscript.php) & # background a subshell, then run the php script
done
i came up with one possible solution, i didnt try it out yet, so it main contain syntax or logic errors. because it is running as a daemon there is a never ending loop around it.
// 3600 seconds or one hour
$timeframe=3600;
// run max 20 times in $timeframe
$runtimes=20;
// minimum delay between two executions
$mindelay=60;
// maxium delay between two executions
$maxdelay=240;
if ($cnt % $runtimes != 0) {
$delay = rand($mindelay,$maxdelay);
System_Daemon::iterate($delay);
$sum += $delay;
$cnt++;
} else {
//final delay till the $timeframe
if ($sum < $timeframe) {
System_Daemon::iterate($timeframe - $sum);
}
$sum=0;
}
its not perfect and u waste some time but i guess its going to fullfill the job.
any comments?