Need your help with my case.
How to make calculating total overtime working hours using condition ?
Example :
using input type,
OT From <input type="text" name="ot_from">
OT To <input type="text" name="ot_to">
Total Hours <input type="text" name="total_hours">
Working days : from 08.00 - 17.00 (normal working days)
If I working until 19.00, should be calculate that I do overtime for 2 hours.
In my rules, from 18.00 - 18.30 not calculate overtime because that's a break time.
So should be my total Overtime hours is 1.5 not 2 hours.
Someone can give me a solution ?
Appreciate your help.
Thank you.
David
Here is what you should do:
1 Split the hours and minutes into a total amount of minutes.
str = "1:23" or other time value
time = parseInt(str.split(":")[0], 10)*60+parseInt(str.split(":")[1], 10)
2 Subtract the finish time from the start time - this will give you the final time.
3 To convert from minutes to hours do the following:
minutes = time%60
hours = parseInt(time/60, 10)
You don't actually need PHP - this can all be done with Javascript, as long as you are not looking to store the data in a database or anything.
If you want to make sure the input of the user is always equal, you should replace the input fields with Select boxes. Then you can make one Select box for the hours and one for the minutes.
To calculate the working hours, you can do a basic calculation like this:
<?php
// Vars of the POST form
$otfrom_hours = $_POST['ot_from_hours'];
$otfrom_minutes = $_POST['ot_from_minutes'];
$otto_hours = $_POST['ot_to_hours'];
$otto_minutes = $_POST['ot_to_minutes'];
// Make a full string for strtotime
$otfrom_string = $otfrom_hours.":".$otfrom_minutes;
$otto_string = $otto_hours.":".$otto_minutes;
// Basic calculation
$start = strtotime($otfrom_string);
$end = strtotime($otto_string);
$elapsed = $end - $start;
echo date("H:i", $elapsed);
?>
Hope it will be usefull!
Related
I would like to know if it would be possible to retrieve a value from an array and use that value for the entire day without changing the value on refresh, it will be used for a discount section. So everyday one of the random values will be taken and that value will be applied as discount % for the next 24 hours. Tomorrow (24 hours later) it will take another value and use that value for the next 24 hours.
I created a logical statement below but isn't working. Any help will be gladly appreciated to complete below function.
//TIME VALUES
date_default_timezone_set('Asia/Dubai');
$currentTime = date('H:i');
$dayStartTime = '00:01';
$dayEndTime = '23:59';
if($currentTime >= $dayStartTime && $currentTime <= $dayEndTime) {
$items = Array("10","15","20");
echo $items[array_rand($items)];
}
Thank you for you time.
Dane
I think you'll want to store a discount somewhere. This will at minimum have 2 fields: amount and expiry.
On load, check if there's a valid discount:
SELECT * FROM discounts WHERE expiry > NOW() LIMIT 1;
If a result is returned, use it. If not, create a new discount with an expiry of 24 hours and use that.
I want to calculate the difference between 2 dates, that works, but is there a function to show a format like this:
1 month
1 year
4 hours
And so on, as you know what I mean.
So perhaps, I ban a user for 2 hours, then it needs to display the user is banned for 2 hours.
By calculating the expire time and banned_at time.
This is what I have now:
date_diff(date_create(\Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $ban->banned_on)), date_create(\Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $ban->expire)))->format('d-m-y');
How can I do this?
$bannedAt = '2015-10-05 10:00:00';
$banLength = 2;
$banTill = Carbon::parse($bannedAt)->addHours($banLength);
$now = Carbon::now();
// If now is less than or equal to banTill then remaining minutes is the difference between now and bantill. Else, the remaining minutes is 0
$remainingBanSeconds = $now->lte($banTill)?$now->diffInSeconds($banTill):0;
echo echo gmdate('H \h\o\u\r\s\, i \m\i\n\u\t\e\s \a\n\d s \s\e\c\o\n\d\s', $remainingBanSeconds);
I am currently trying to get a countdown clock using a unix timestamp value (remaining seconds) to H:M:S format, in order to sync my client side timers.
The value that I need to change to H:M:S has already been calculated as the time remaining in the countdown.
Let
$remaining_time
be our value of remaining seconds in the countdown.
Currently, here is what I have:
$H = floor($remaining_time / 3600);
$M = floor(($remaining_time - ($H*3600)) / 60);
$S = floor($remaining_time - ($H*3600 - ($M*60)));
I believe the hours/minutes is pretty close... but the seconds seems to be off. For example, I am getting results like this
$remaining_time = 4135;
Result: Time Remaining at price formatted: H:1 M:8 S:1015
Any information is appreciated - again, I need the remaining seconds in hours, minutes, and seconds.
Use gmdate, example:
<?php
$remaining_time = 4135;
echo "Time Remaining at price formatted: ".gmdate("\H:H \M:i \S:s",4135);
//Result: Time Remaining at price formatted: H:01 M:08 S:55
?>
I'm trying to calculate an average interval between TIMESTAMP (YY-MM-DD HH:MM:SS) records in a column (hit_date) from a table.
I did this in MySql:
SELECT DATEDIFF(MAX(hit_date), MIN(hit_date)) / (COUNT(hit_date) - 1) AS hitavg FROM my_table
This returns a value, ie 135.50.
Then, I did this in PHP to show results:
$value = ($res_from_mysql_query);
$days = (int) $value;
$hours = 24 * ($value - $days);
echo "$days Days, $hours Hours";
and my result is:
135 Days, 0 Hours.
But this is not the correct result... what's wrong?
Have I to use TIMEDIFF in MySql? If yes, I got a total different value... so, how can I implement my PHP script?
How can I correctly show Days and Hours for this interval?
Please, help me to improve this, any help would be really appreciated!
what about like that code:
SELECT TIMESTAMPDIFF(HOUR, MIN(hit_date), MAX(hit_date)) / (COUNT(*)-1) AS hitavg FROM my_table
TIMESTAMPDIFF(UNIT, DATETIMEEXP1, DATETIMEEXP2) returns the difference in HOURS between DATETIMEEXP1 and DATETIMEEXP2.. for each "select" the query finds the date of first and last hit (visit) and total visit's count, then calcute arithmetic average.
Then in PHP is easy way to display the difference...
Maybe you should look at these MySQL ref:
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timediff
All employees in/out data entry is fetching in this format:
logtype time Date
------------------------------------------------------
start_time [come office] 10:30 11-11-2010
end_time [go to lunch] 14:00 11-11-2010
start_time [come back from lunch] 15:00 11-11-2010
end_time [out from office] 20:00 11-11-2010
Question:
how can I calculate 'start time', 'end time', 'total time including lunch time' and 'total time excluding lunch time'
start time
end time
total time including lunch time
total time excluding lunch time
I've thought about it, and I don't see a way of doing this without iterating each item and making the calculations yourself. You could add a "Day" column (e.g. DAYOFMONTH, DAYOFWEEK, or DAYOFYEAR) then begin going over the login/logout times by "time" ascending. The only problem would be "open-ended" times (scenarios where a user didn't punch in/out).
Most time tables I've seen has eliminated this with using (typically) 4 columns.
Work_Day
Login_Time
Logout_Time
Hours_Worked
Then you can conclude if there was a value missing (one of the time fields would be NULL) and can do a quick query against the table for number of hours worked (or on a weekly bases with a quick column addition using Work_Day as the root of the calculation).
To answer the original question though, the pseudo-code would go something like this:
// $query = mysql_query(....) // <-- populate this
$times = Array('start'=>null,'end'=>null);
$result = Array('start_time'=>null,'end_time'=>null,'total_wo_lunch'=>0,'total_w_lunch'=>0);
while ($log = mysql_fetch_array($query)){
{
$_ = $log['date'].' '.$log['time'].':00';
$_datetime = strtotime($_);
if (is_null($result['start_time'])) $result['start_time'] = $_datetime;
else $result['end_time'] = $_datetime;
if (is_null($times['start'])) $times['start'] = $_datetime;
else if (is_null($times['end'])) {
$times['end'] = $_datetime;
$diff = ($times['end']-$times['start'])/3600; // convert to hours
$result['total_wo_lunch'] += $diff;
$times['start'] = null;
$times['end'] = null;
}
}
$result['total_w_lunch'] = ($result['end_time']-$result['start_time'])/3600; // convert to hours
print_r($result);
EDITv2
This is an update with code that works. My only issue here is with a bit of rounded on the time card (the worked 8.5 hour day is coming in at 8.53, though I don't see why. Same with the overall 9.5 hour day). other than that it seems to work fine. Let me know if you'd like to see other changes, and my apologies for posting untested code. I was more-or-less going for conceptual.