I want to calculate the hours in the attendance sheet
but I don't want to calculate the hours and minutes that are not between these two times that are: 08:00 to 14:00
for example:
employee A arrived to work at 09:00 and left at 16:00
the result should be 5 hours
and if Employee B arrived to work at 8:30 and left at 14:00
the result should be 5hours and 30 mins
kind regards
here is your answer
<?php
$restriction_start = strtotime("30-Mar-2015 08:00:00");
$restriction_end = strtotime("30-Mar-2015 14:00:00");
$start_time = strtotime("30-Mar-2015 09:00:00");
$end_time = strtotime("30-Mar-2015 16:00:00");
echo calculateTime($start_time, $end_time, $restriction_start, $restriction_end);
function calculateTime($start_time, $end_time, $restriction_start, $restriction_end)
{
$start = $restriction_start;
if($start_time>$restriction_start)
{
$start = $start_time;
}
$end = $restriction_end;
if($end_time<$restriction_end)
{
$end = $end_time;
}
$time_diff = $end-$start;
$output = gmdate("H:i:s", $time_diff);
return $output;
}
?>
You can use Datetime object to do that. There is the documentation : http://php.net/manual/en/datetime.diff.php
<?php
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
?>
var $NbrSec = mktime($hour1, $minute1, $second1, $month1, $day1, $year1)
- mktime($hour2, $minute2, $second2, $month2, $day2, $year2);
var $Hr = ((int)($NbrSec / 60)) * 60;
var $Mn = $NbrSec % 60;
echo $Hr . ":" . $Mn;
Related
I'm trying to calculate the difference of two times. I'm using this method to return the difference in minutes.
$datetime1 = new DateTime('9.00am');
$datetime2 = new DateTime('10.15am');
$interval = $datetime1->diff($datetime2);
$timeDuration = $interval->format('%im');
However, the $timeDuration returns 15m instead of 75m.
Any help that could correct the code to let it return the exact correct difference in minutes ?
Because the difference between 9am and 10:15am is 1 hour ($interval->h) and 15 minutes ($interval->i), not 75 minutes literally.
If you wish to get the total number of minutes, you have to calculate it:
$differenceInMinutes = $interval->i + ($interval->h * 60);
If you wish to have more universal solution, I would go for the unix time:
$differenceInSeconds = abs($datetime1->format('U') - $datetime2->format('U'));
$differenceInMinutes = ceil($differenceInSeconds / 60);
The simple way to get different in minutes
<?php
$start = date_create('1990-01-01 09:00:00');
$end = date_create('1990-01-01 10:15:00');
$interval=date_diff($end, $start);
$hours = $interval->format('%h');
$minutes = $interval->format('%i');
$in_minutes = $hours * 60 + $minutes;
echo 'Diff. in minutes is: '.$in_minutes;
?>
When you are trying to calculate the difference between two DateTime's like $datetime1 = new DateTime('9.00am'); and $datetime2 = new DateTime('10.15am'); you know that the difference is 75 minutes, but the resulto of diff() method is 1 hour for $interval->h and 15 minutes for $interval->i. So this is how you can calculate it:
$datetime1 = new DateTime('9.00am');
$datetime2 = new DateTime('10.15am');
$interval = $datetime1->diff($datetime2);
$hours = $interval->format('%h');
$minutes = $interval->format('%i');
$difference_in_minutes = $hours * 60 + $minutes;
echo 'Difference in minutes is: ' . $difference_in_minutes . "\n";
There a working snipped in this link
I have two dates like
2016-01-25 07:33:54 and current date 30-01-2016 01.27.00
I want to get difference between these dates with dates in number of hours, number of minutes and number of seconds .
$fromDate = "2016-01-25 07:33:54";
$toDate = "30-01-2016 01.27.00";
$difference = strtotime($toDate)-strtotime($fromDate);
echo "Seconds : ".$difference."<br>";
echo "Minutes : ".($difference/60)."<br>";
echo "Hours : ".($difference/(60*60));
Or
A Easy way
$fromDate = "2016-01-25 07:33:54";
$toDate = "30-01-2016 01.27.00";
$datetime1 = new DateTime($toDate);
$datetime2 = new DateTime($fromDate);
$interval = $datetime1->diff($datetime2);
$days = $interval->d;
$hours = $interval->h;
$minutes = $interval->i;
$seconds = $interval->s;
echo "$days days, $hours Hrs, $minutes Mins, $seconds Sec";
http://php.net/manual/en/function.strtotime.php
use strtotime to find time difference between two dates.
http://php.net/manual/en/class.datetime.php
You can also try this.
checkout my code below.
<?php
$datetime1 = new DateTime('2014-02-16 04:04:26 AM');
$datetime2 = new DateTime('2014-02-11 05:36:56 AM');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days')." days ".$interval->format('%h')." Hours ".$interval->format('%i')." Minutes".$interval->format('%s')." Seconds";
?>
I have the following code:
$sStart = date('Y-m-d H:i:s', strtotime($row['sStart']));
$sEnd = date('Y-m-d H:i:s', strtotime($row['sEnd']));
$sStartDate = new DateTime($sStart);
$sEndDate = new DateTime($sEnd);
$diff = date_diff($sStartDate, $sEndDate, true);
$dur = $diff->format('%h:%i');
$row['sStart'] and $row['sEnd'] are datetimes from a MySQL database.
How can I tell if $diff is over 24 hours long?
The $diff is of type DateInterval.
I think that this will give you the number of hours using the result of the date_diff:
$hours = ($diff->days * 24) + $diff->h;
if ($hours > 24) {
// etc ..
}
Since 24 hours is 1 day, you can use the %a modifier to the DateInterval::format call to calculate the number of days the interval crosses:
$sStartDate = DateTime::createFromFormat('Y-m-d H:i:s', '2013-05-13 21:56:00');
$sEndDate = DateTime::createFromFormat('Y-m-d H:i:s', '2013-05-14 21:56:01');
$diff = date_diff($sStartDate, $sEndDate, true);
$days = $diff->format('%a');
if (1 <= $days) {
echo '24 hours or more.';
} else {
echo 'less than 24 hours.';
}
See it live.
I currently have a list of time diffs in G:i format.
I would like to add them all up but when i try to it resets at 24 back to 0.
How can i get a result like: 54:45 for example?
Thanks in advance.
date_default_timezone_set('UTC');
$username = $row['username'];
$begintime = $row['begintime'];
$endtime = $row['endtime'];
$begintime = new DateTime($begintime);
$begintime = $begintime->format('H:i');
$endtime = new DateTime($endtime);
$endtime = $endtime->format('H:i');
$difference = strtotime($endtime) - strtotime($begintime);
$difference = date('G:i', $total);
$total_hours += $difference;
Don't use date() to format absolute times. Since $difference contains the time difference in seconds, it's just a matter of simple arithmetics:
$hours = floor( $difference / 60 / 60 );
$minutes = ( $difference / 60 ) % 60;
echo "$hours:$minutes";
You can actually "trick" DateTime into doing absolute times by adding the differences to an arbitrary date:
$foo = new DateTime();
$bar = new DateTime();
foreach ($intervals as $interval) {
$begin = $interval['begin'];
$end = $interval['end'];
$foo->add($begin->diff($end));
}
// Will be the total time of all intervals, as an interval itself
$total = $foo->diff($bar);
I want to subtract a date in the future from todays date, I want it to display both the day, hour and minutes until the target date.
Here is my code
<?php
date_default_timezone_set('Europe/London');
$format = "h:i d";
$date = date($format);
$target = date($format, mktime(0,0,0,12,8,2011));
echo date($format, $target-$date);
?>
Kind regards,
Adam
Don't use date for operations, it's meant for displaying dates. Instead subtract 2 timestamps:
...
$date = mktime(now...);
$target = mktime(0,0,0,12,8,2011);
echo date($format, $target - $date);
But you have to realize timestamps start in 1970 and end in 2038, therefore, for example, 2011 - 2007 = 1974.
More suitable in your case would be date_diff as #Kerrek SB suggested in comment.
Example (from php.net):
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days'); // +2 days
Maybe this will help you
<?php
$target = mktime (23, 34, 0, 9, 28, 2009);
$today = mktime();
$difference =($target-$today) ;
$days = $difference / 3600 / 24;
$difference = $difference - floor($days) * 3600 * 24;
$hours = $difference / 3600;
$difference = $difference - floor($hours) * 3600;
$minutes = $difference / 60;
echo "Days: ";
echo floor($days);
echo "<br />";
echo "Hours: ";
echo floor($hours);
echo "<br />";
echo "Minutes: ";
echo floor($minutes);
?>