I'm using a time clock system which, by default, records only the employee's entry and exit times. I'm customizing it so that it's possible to also record break times but I'm having trouble getting the break time to be subtracted from the total time.
This snippet of code is used to register the time between the check-in and check-out:
$time1 = Carbon::createFromFormat("Y-m-d H:i:s", $timeIN);
$time2 = Carbon::createFromFormat("Y-m-d H:i:s", $timeOUT);
$th = $time1->diffInHours($time2);
$tm = floor(($time1->diffInMinutes($time2) - (60 * $th)));
$totalhour = ($th.".".$tm);
The variable ($totalhour) receives the total value between the input register and the output register. It sends to the database in H.i format (hour.minutes), then another page searches for this information in the database and replaces the point (.) with (hr).
Based on this code, I did the same to get the interval start and end timestamps. I was able to get the time between the start time and end time interval with the code below:
$breakstart = table::attendance()->where([['idno', $idno]])->value('breakstart');
$breakend = table::attendance()->where([['idno', $idno]])->value('breakend');
$one = Carbon::createFromFormat("H:i:s", $breakstart);
$two = Carbon::createFromFormat("H:i:s", $breakend);
$breakone = $one->diffInHours($two);
$breaktwo = floor(($one->diffInMinutes($two) - (60 * $breakone)));
$totalbreak = $breakone.".".$breaktwo;
The $totalbreak variable stores the time taken between the start and end of the break. I was also successful in getting the time between this interval.
Now, I need the total time to be done by subtracting the time obtained from the record at the beginning of the interval to the record at the end of the interval.
I did with this code and got good result up to a point. Could you give me tips on how to get an assertive result in this case?
$totalhour = ($th.".".$tm) - ($totalbreak);
I tried to get the total time by subtracting the break time, but without success.
Related
i am new to stackOverflow, i was wondering how could i get the missing time date to a microtime(true) starting from another microtime(true), and get it formatted like that (H:i:s).
This is my code:
$rewardCountdown = microtime(true) - $this->dailyRewardTime; // daily reward is another microtime(true)
$rewardAvailable = $rewardCountdown >= 60 * 60 * 24; // check if 24h are gone so we can get reward
Basically i want to get the $rewardCountdown in this format (H:i:s)
I tried and somehow got something like that but i was getting the time increasing instead of decreasing
The current microtime minus a previous microtime would be a positive value, of the number of seconds passed between the two times.
If you want to convert seconds into Hours, Minutes and Seconds, you can simply use gmdate.
$rewardCountdown = microtime(true) - $this->dailyRewardTime;
$date = gmdate('H:i:s', $rewardCountdown);
I am making a game mode in which I am trying to get the time a player has arrived at their destination after starting the mode and to do this I am using the insert of a date when it starts the mode it inserts a date and after reaching the your destination it registers another date and with both dates it calculates the time it took to get to the destination, with this I'm using date H:i:s (hours, minutes, seconds) but I need to take the time out and leave milliseconds after seconds example: i:s:u (minutes, seconds, milliseconds) but I'm not able to do this, I've tried it in several ways, basically everything works as follows:
1. I add in the player array a current date with hour, minutes, seconds;
$this->game[$player->getName()] = ["start" => strtotime('now')];
2. After the Player arrives at his destination he calculates the time of his trajectory creating another current date with already registered and using date and mktime to do the join and give a visual of time to the player;
$time = date('H:i:s', mktime(0, 0, str_replace("-", "", $this->game[$player->getName()]["start"] - strtotime('now'))));
3. Send the pretty message to the player about the time of his trajectory then time will be something like this: 01:45:23 (minute:seconds:milliseconds).
$player->sendMessage("You beat your time record by ".$time);
This is my method of doing, if you have another better method with the milli seconds added I accept the suggestion! Maybe there might be some errors in my code that I'm still not sure if they work correctly as the subtraction to calculate and join the current time with the previous one, tell me if it's right and if it is not correct correct me or do better. Thank you
Use microtime which returns the current Unix timestamp with microseconds
$game = [];
$game['start'] = microtime(true);
// Do stuff
sleep(3); // Without the sleep, start and end are the 'same'
$game['end'] = microtime(true);
$elapsedTime = ($game['end'] - $game['start']);
$minutes = floor($elapsedTime / 60);
$seconds = $elapsedTime % 60;
$milliseconds = number_format($elapsedTime - floor($elapsedTime),3);
Let's say I have a table and it consists a column of next_update (which is in a date format), time_left (which is in the unit of days). How could I program it for example the next_update is 27/02/14 and the time_left is 3 days for today(24/02/14) view in a php webpage but for tomorrow view in the php page will be automatically deducted to 2 days. I'm using postgresql as my database and php as the web interface. The main problem now is how can I make the value of time_left be minus by the next_update with the current date.
I've gone through some basic manual but still have no idea to set this up. Sincerely thank you all for any help.
Try this code
<?php
$now = time();
$next_update = strtotime("2014-02-27");
$datedifferent = $next_update - $now;
$days = floor($datedifferent/(60*60*24));
if($days > 0) {
echo $days.' more days you have';
}
?>
SELECT next_update, next_update - now() as time_left FROM <your_table>
You may also want to apply function EXTRACT to subtract necessary time units from interval (documentation)
I am creating a online examination portal where the students get a total of 3 hrs to answer the question , i want to save the value of the countdown timer in the database so that if there is a power failure then the timer can again start from the last saved value
You should track the date when the timer starts (which can be a mysql datetime column) and now you can calculate the time which is past.
Here an example for calculate the time:
<?php
// load the date from the database
$timeDate = strottime($data['created_at']);
$now = time();
$diff = $now - $timeDate;
// $diff is now an \DateInterval
if ($diff > 10800) { // If the difference more than 3 hours
// show some errors or do whatever you want :)
}
I´m making a simple time management system feature and I want to add
task and estimated minutes.
So if I add into the field "finish sending e-mail to John" and "23" (as minutes) it goes into
mysql as $sql = "INSERT INTO schedule (task, time, timestamp) VALUES ('$_POST[task]','$_POST[time]','$_POST[timestamp]')";
The output would be " Finish sending e-mail to John 21:02 - 21:25 "
so if next task takes 7 minutes it will be from 21:26 - 21:33" (take notice
of the first task and so and and so forth
I tried echo date('H:i', strtotime('+["time"] minutes', ));
but it doesn´t
work and I don´t know how the next record would take notice of the next one
is this possible?
What you tried above is almost correct, except that you might mean $_POST['time'] instead of ["time"], making your code: echo date ('H:i', strtotime('+{$_POST["time"]} minutes'));. But that will give you the time minutes after current time, not after the starting time.
To get the end time, you need to convert your start time (I assume it's $_POST['timestamp']) into UNIX timestamp, then add the task's length and get the UNIX timestamp from it.
Give this a try:
// Remember to check for valid user input. I'll leave that for you.
$task = $_POST['task'];
$time = $_POST['time'];
$timestamp = $_POST['timestamp'];
// Get the time here
$start_time = date ("H:i", strtotime ($timestamp));
$end_time = date ("H:i", strtotime ($timestamp) + $time * 60);
echo "{$task} {$start_time}-{$end_time}\n";