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');
Related
I am looping over a series of hours using DatePeriod. This works for periods not containing DST changes. It works for a missing hour during switch to DST. But it does NOT work for a double hour during switch to non-DST season.
Base code:
$zone = new DateTimeZone('Europe/Amsterdam');
$start = new DateTime('2016-01-01', $zone);
$end = new DateTime('2016-01-02', $zone);
$int = new DateInterval('PT1H');
$period = new DatePeriod($start, $int, $end);
foreach($period as $point)
echo $point->format('Y-m-d H:i') . "\n";
This gives:
2016-01-01 00:00
2016-01-01 01:00
2016-01-01 02:00
...
2016-01-01 23:00
For $start = '2016-03-27' and $end = '2016-03-28' it gives:
2016-03-27 00:00
2016-03-27 01:00
2016-03-27 03:00
....
This is expected because 02:00 does not exist that day.
But for $start = '2016-10-30' and $end = '2016-10-31' it does not work:
2016-10-30 00:00
2016-10-30 01:00
2016-10-30 02:00
2016-10-30 03:00
....
We expect a double hour. It is not there. Outputting the timestamps using $point->format('U') shows that the timestamps are correct and there is a jump of 7200 seconds between 1 and 2 o'clock instead of the expected 3600.
Why does PHP not follow my specified interval of 1 hour? How can I get the period to include this hour? I have tried $int = 'PT60M' and similar, but that does not change anything.
I am slightly confused to build Time-slot logic dynamically could you please help me out.
I need to display some list of time-slot but each time slot will be of two hour. please see list inline below
00:00 02:00
02 04
04 06
06 08
08 10
10 12
12 14
14 16
16 18
18 20
20 22
22 00
Code
public function getTimeSlot(){
$custom = array();
$M = 'AM';
for($i=0;$i<=23;$i++):
$time1 = strtotime($i.':00:00');
$time2 = strtotime(($i*2).':00:00');
$diff = $time2 - $time1;
$custom[] = date('H:i:s ', $diff);
endfor;
pr($custom);
exit();
}
Your question seems vague. Is it like this?
00:00 02:00
02:00 04:00
04:00 06:00
06:00 08:00
08:00 10:00
10:00 12:00
12:00 14:00
14:00 16:00
16:00 18:00
18:00 20:00
20:00 22:00
22:00 00:00
If I would just print a simple time, I would do something like:
$start = 0; $end = 22;
$time_slot = range($start, $end, 2);
$time_slot = array_map(function($time) use ($end){
$next = ($time != $end) ? $time + 2 : 00;
$times = array(sprintf("%02s:00", $time), sprintf("%02s:00", $next));
return $times;
}, $time_slot);
foreach($time_slot as $time) {
echo "$time[0] $time[1] <br/>";
}
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.
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))
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