im calculating the hours worked by an employee and need to know if the employee have extra hours worked or if the employee donĂ½ work the total hours corresponding with his profile.
For example:
Day From_Hour To_Hour Hs_Worked Extra_time PHP_Return
1 11:00 19:30 08:30 00:00 00:00
2 11:30 19:30 08:00 -00:30 23:00
3 11:00 19:45 08:45 -00:15 23:45
4 11:00 19:55 08:55 +00:10 00:10
I have all the values, but need to calulate the Extra_time and i no have problem with positive values, but when i am trying to substract more time of that i have, php returns an equivalent to a previous day.
How can i add and substract times having only possitive or negative times (w/out previous days hours)?
$start = DateTime::createFromFormat('H:i', '11:30');
$start->add(new DateInterval('PT8H30M'));
$end = DateTime::createFromFormat('H:i', '19:30');
$diff = $start->diff($end);
echo $diff->format('%r%H:%I');
Demo
This just adds 8 1/2 hours to the start time and then gets the difference from the end time.
Related
I have a date fields in database that have the day of the entry. The fields are starttime and stoptime, which define their schedule
For today of posting the start time is 2018-12-17 00:00:00 and the stop time is 2018-12-17 11:59:59
Example start times:
2018-12-17 15:30:00,
2018-12-24 06:00:00,
2018-12-17 10:00:00,
2018-12-17 09:00:00
and stop times (in the same order)
2018-12-17 16:00:00,
2018-12-24 07:00:00,
2018-12-17 12:30:00,
2018-12-17 10:30:00
it will show only 2018-12-17 09:00:00/2018-12-17 10:30:00
or everything else but the 1 above if i switch up the operation signs in if statement.
$starttime = date("Y-m-d H:i:s", strtotime('last monday', strtotime('next sunday')));
$stoptime = strtotime(date("Y-m-d H:i:s", strtotime($starttime))) + 43199;
$stoptime = date("Y-m-d H:i:s", $stoptime);
$stmt2 = $db->prepare("SELECT * FROM schedule WHERE day = :day");
$stmt2->execute(array(':day' => "Monday"));
while($row2 = $stmt2->fetch()){
if($row2['starttime'] >= $starttime AND $row2['stoptime'] <= $stoptime){
//data gets displayed here.
}
}
It looks like you have a misinterpreted date/time. From what you are supplying of 43199, this is 1 second LESS then 12HOURS, not a 24 hour day. So what you are getting are those entries that are before 12 mid-day time instead of 12 in the morning. On a 24-hour clock you would be at 86,400 for 24 hours... or 86,399 for 1 second less.
Additionally, you could update your query to just care about the start time as anytime within the starting date in question. If the ending time happens to roll-over to the next date, I am sure you want that in the result set too.
SELECT *
FROM schedule
WHERE startTime >= '2018-12-17'
and starttime < date_add( '2018-12-17', interval 7 days)
Now, I am doing LESS than 7 days in the above sample. So lets look at it.. The beginning date/time of '2017-12-17' is IMPLIED at hour:min 00:00 at midnight. By doing LESS than 7 days, it covers the entire week up to 11:59:59 PM (or 24hour clock 23:59:59) on the following Sunday. So it will be all LESS then Monday of the following week. Then you don't have to worry about formatting your specific strings in the query and then testing. Just get the results and go.
My question: if the date is on Monday then my timer should start from 8:30 am and should calculate the time difference from 8:30 till date assigned, in this case its 2017-06-02 14:20:00 for first one. So time difference should be 5 hours 50 min.
Second case, date created on 2017-06-02 09:50:00 and date assigned is: 2017-06-03 13:20:00. SO it should calculate from 9:50 till 9:00pm and again start from 8:30 till 13:20:00 (if next day lies on mon-sat. If next day is sun then timer should calculate from 11am till 1:20pm. and should give me duration in hours and minutes.
How would I do that? Its in Php & MySQL. I am not any frameworks or CMS systems. Its native php.
My Data:
Date Created: 2017-06-02 02:50:00
Date Assigned: 2017-06-02 14:20:00
Date Created: 2017-06-02 09:50:00
Date Assigned: 2017-06-03 13:20:00
Mon - Sat = 8:30am - 9:00pm
Sunday = 11am - 5pm
Thank you in advance.
Edited: This function checks first what day is your dateassigned, then calculates the time difference based on your criteria.
<?php
echo getTimeLapsedCustom('2017-06-02 14:20:00') . "<br>";
echo getTimeLapsedCustom('2017-06-03 13:20:00') . "<br>";
function getTimeLapsedCustom($time){
$timecreated = '';
$timeassigned = $time;
$datetime2 = DateTime::createFromFormat('Y-m-d H:i:s', $timeassigned);
if($datetime2->format('D') === 'Sun'){
$timecreated = $datetime2->format('Y-m-d') . ' 11:00:00';
}else{
$timecreated = $datetime2->format('Y-m-d') . ' 08:30:00';
}
$datetime1 = DateTime::createFromFormat('Y-m-d H:i:s', $timecreated);
$interval = $datetime1->diff($datetime2);
return $interval->format('%h hours %i min');
}
Results:
5 hours 50 min
4 hours 50 min
In this case hours_start will be 08:00:00 and hours_end will be 14:00:00
here's the code i'm using to generate a list of 30 minute time slots between start and end
while($row = $q->fetch()){
$hours = $row['hours_end'] - $row['hours_start']; //amount of hours working in day
for($i = 0; $i < $hours * 2; $i++){ // double hours for 30 minute increments
$minutes_to_add = 1800 * $i; // add 30 - 60 - 90 etc.
$timeslot = date('h:i:s', strtotime($row['hours_start'])+$minutes_to_add);
echo "
<tr>
<td>" . date('h:i A', strtotime($timeslot)) . "</td>
</tr>";
}
}
this is producing:
08:00 AM
08:30 AM
09:00 AM
09:30 AM
10:00 AM
10:30 AM
11:00 AM
11:30 AM
12:00 PM
12:30 PM
01:00 AM
01:30 AM
as you can see, it is functioning as (i) expected until it gets to (what should be) 1 PM then it switches back to AM. not sure whats going on here.
Its because you are setting $timeslot using h which is only a 12 hour format without appending am or pm. Then taking that 12 hour format and running it through strtotime which expects 24 hour format if am or pm is not present. Hence anything after 12 becomes am again.
You need to use:
$timeslot = date('H:i:s', strtotime($row['hours_start'])+$minutes_to_add);
OR
$timeslot = date('h:i:s a', strtotime($row['hours_start'])+$minutes_to_add);
Your date format is using h instead of H. The lowercase h is 12 hour format
Use a capital H instead for 24 hours.
date('H:i A', strtotime($timeslot))
Is there a function to count the total hour minutes between multiple dates?
I have multiple times like these:
10:00 18:00 | 15:00 23:00 | 00:00 00:00 | 08:30 16:30 | 00:00 00:00 | 16:00 19:00 | 00:00 00:00
As you can see some can be empty, now i need to know the total amount of hours and minutes i worked that week. Does somebody know a solution?
Thanks!
Here is an example solution including parsing of the input in your question. It does not use the DateTime class, but performs a super simple calculation to calculate the total hours.
$shifts = "10:00 18:00 | 15:00 23:00 | 00:00 00:00 | 08:30 16:30 | 00:00 00:00 | 16:00 19:00 | 00:00 00:00";
$shifts= explode('|', $shifts);
$sum = 0;
foreach($shifts as $shift) {
$times = explode(' ', trim($shift));
$start = explode(':', $times[0]);
$end = explode(':', $times[1]);
$sum += ($end[0] - $start[0]) + ($end[1] - $start[1]) / 100 / 0.6;
}
echo $sum; // prints the result 27
I hope that this will help you.
// Create two new DateTime-objects...
$date1 = new DateTime('2006-04-12T12:30:00');
$date2 = new DateTime('2006-04-14T11:30:00');
// The diff-methods returns a new DateInterval-object...
$diff = $date2->diff($date1);
// Call the format method on the DateInterval-object
echo $diff->format('%d Day and %h hours');
I have this issue with strtotime in my PHP code that it is wrong sometimes (only for some timezones) and it is correct for others!!
I cannot get my head around it.
I have set the <?php date_default_timezone_set('GMT'); ?> at the top of my page as well but that doesn't help!
basically what it does is that it will add or subtract the offset/3600 value to the set time in $time1 = strtotime('00:00'); depending on the if and else if conditions.
the offset/3660 value is the time difference between two timezones!
the code bellow works for some locations and it doesn't for others! basically it will add an extra 1-2 hours or takes off/subtract an extra 1-2 hours some times (not all the time).
i.e. the time difference between Abidjan and london is -1.
the time (value) that should be shown is 23:00 as 00:00 - 01:00 = 23:00. but the value is shown is 00:00.
However as i mentioned it works for some timezones.
i.e. for the time difference between New York and London which is -5 the code works and it shows 19:00 as 00:00 - 05:00 = 19:00
could someone please shed a light on this?
here is the code in question:
<?php
$time1 = strtotime('00:00');
if (0 > $offset)
{
// For negative offset (hours behind)
$hour_dif = date('H:i', strtotime($time1 -$offset/3600));
$time1 = "{$hour_dif}";
}
elseif (0 < $offset)
{
// For positive offset (hours ahead)
$hour_dif = date('H:i', strtotime($time1 +$offset/3600));
$time1 = "{$hour_dif}";
}
else
{
// For offsets in the same timezone.
$time1 = "in the same timezone";
}
echo "{$time1}";
?>
Well, since strtotime() already returns a timestamp and date() expects one you could just do
$hour_dif = date('H:i', ($time1 - ($offset*3600)));
or
$hour_dif = date('H:i', ($time1 + ($offset*3600)));
respectively, to remove or add the correct amount of seconds from/to the timestamp.
I am also assuming that $offset is the offset in hours, so you would have to multiply by 3600 to get the amount of seconds, not divide.
Alright, after testing your code and thinking through it some more, it became obvious.
With a negative offset like -1 you're going to calculate $time1 - (-1) * 3600 and we all know that a double negation is positive...
So in fact, your code can be condensed to:
$time1 = strtotime('00:00');
if ($offset == 0)
$time1 = "in the same timezone";
else
{
// For positive offset (hours ahead)
$hour_dif = date('H:i', ($time1 + ($offset*3600)));
$time1 = "{$hour_dif}";
}
echo "{$time1}\n";
and should work as expected:
cobra#box ~ $ for i in {-24..24}; do php test.php $i; done;
00:00
01:00
02:00
03:00
04:00
05:00
06:00
07:00
08:00
09:00
10:00
11:00
12:00
13:00
14:00
15:00
16:00
17:00
18:00
19:00
20:00
21:00
22:00
23:00
in the same timezone
01:00
02:00
03:00
04:00
05:00
06:00
07:00
08:00
09:00
10:00
11:00
12:00
13:00
14:00
15:00
16:00
17:00
18:00
19:00
20:00
21:00
22:00
23:00
00:00