Calculate number of hours between 2 dates in PHP - php
How do I calculate the difference between two dates in hours?
For example:
day1=2006-04-12 12:30:00
day2=2006-04-14 11:30:00
In this case the result should be 47 hours.
The newer PHP-Versions provide some new classes called DateTime, DateInterval, DateTimeZone and DatePeriod. The cool thing about this classes is, that it considers different timezones, leap years, leap seconds, summertime, etc. And on top of that it's very easy to use. Here's what you want with the help of this objects:
// 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('%a Day and %h hours');
The DateInterval-object, which is returned also provides other methods than format. If you want the result in hours only, you could to something like this:
$date1 = new DateTime('2006-04-12T12:30:00');
$date2 = new DateTime('2006-04-14T11:30:00');
$diff = $date2->diff($date1);
$hours = $diff->h;
$hours = $hours + ($diff->days*24);
echo $hours;
And here are the links for documentation:
DateTime-Class
DateTimeZone-Class
DateInterval-Class
DatePeriod-Class
All these classes also offer a procedural/functional way to operate with dates. Therefore take a look at the overview: http://php.net/manual/book.datetime.php
$t1 = strtotime( '2006-04-14 11:30:00' );
$t2 = strtotime( '2006-04-12 12:30:00' );
$diff = $t1 - $t2;
$hours = $diff / ( 60 * 60 );
To provide another method for DatePeriod when using the UTC or GMT timezone.
Count Hours https://3v4l.org/Mu3HD
$start = new \DateTime('2006-04-12T12:30:00');
$end = new \DateTime('2006-04-14T11:30:00');
//determine what interval should be used - can change to weeks, months, etc
$interval = new \DateInterval('PT1H');
//create periods every hour between the two dates
$periods = new \DatePeriod($start, $interval, $end);
//count the number of objects within the periods
$hours = iterator_count($periods);
echo $hours . ' hours';
//difference between Unix Epoch
$diff = $end->getTimestamp() - $start->getTimestamp();
$hours = $diff / ( 60 * 60 );
echo $hours . ' hours (60 * 60)';
//difference between days
$diff = $end->diff($start);
$hours = $diff->h + ($diff->days * 24);
echo $hours . ' hours (days * 24)';
Result
47 hours (iterator_count)
47 hours (60 * 60)
47 hours (days * 24)
Count Hours with Daylight Savings https://3v4l.org/QBQUB
Please be advised that DatePeriod excludes an hour for DST but does not add another hour when DST ends. So its usage is subjective to your desired outcome and date range.
See the current bug report
//set timezone to UTC to disregard daylight savings
date_default_timezone_set('America/New_York');
$interval = new \DateInterval('PT1H');
//DST starts Apr. 2nd 02:00 and moves to 03:00
$start = new \DateTime('2006-04-01T12:00:00');
$end = new \DateTime('2006-04-02T12:00:00');
$periods = new \DatePeriod($start, $interval, $end);
$hours = iterator_count($periods);
echo $hours . ' hours';
//DST ends Oct. 29th 02:00 and moves to 01:00
$start = new \DateTime('2006-10-28T12:00:00');
$end = new \DateTime('2006-10-29T12:00:00');
$periods = new \DatePeriod($start, $interval, $end);
$hours = iterator_count($periods);
echo $hours . ' hours';
Result
#2006-04-01 12:00 EST to 2006-04-02 12:00 EDT
23 hours (iterator_count)
//23 hours (60 * 60)
//24 hours (days * 24)
#2006-10-28 12:00 EDT to 2006-10-29 12:00 EST
24 hours (iterator_count)
//25 hours (60 * 60)
//24 hours (days * 24)
#2006-01-01 12:00 EST to 2007-01-01 12:00 EST
8759 hours (iterator_count)
//8760 hours (60 * 60)
//8760 hours (days * 24)
//------
#2006-04-01 12:00 UTC to 2006-04-02 12:00 UTC
24 hours (iterator_count)
//24 hours (60 * 60)
//24 hours (days * 24)
#2006-10-28 12:00 UTC to 2006-10-29 12:00 UTC
24 hours (iterator_count)
//24 hours (60 * 60)
//24 hours (days * 24)
#2006-01-01 12:00 UTC to 2007-01-01 12:00 UTC
8760 hours (iterator_count)
//8760 hours (60 * 60)
//8760 hours (days * 24)
your answer is:
round((strtotime($day2) - strtotime($day1))/(60*60))
The easiest way to get the correct number of hours between two dates (datetimes), even across daylight saving time changes, is to use the difference in Unix timestamps. Unix timestamps are seconds elapsed since 1970-01-01T00:00:00 UTC, ignoring leap seconds (this is OK because you probably don't need this precision, and because it's quite difficult to take leap seconds into account).
The most flexible way to convert a datetime string with optional timezone information into a Unix timestamp is to construct a DateTime object (optionally with a DateTimeZone as a second argument in the constructor), and then call its getTimestamp method.
$str1 = '2006-04-12 12:30:00';
$str2 = '2006-04-14 11:30:00';
$tz1 = new DateTimeZone('Pacific/Apia');
$tz2 = $tz1;
$d1 = new DateTime($str1, $tz1); // tz is optional,
$d2 = new DateTime($str2, $tz2); // and ignored if str contains tz offset
$delta_h = ($d2->getTimestamp() - $d1->getTimestamp()) / 3600;
if ($rounded_result) {
$delta_h = round ($delta_h);
} else if ($truncated_result) {
$delta_h = intval($delta_h);
}
echo "Δh: $delta_h\n";
//Calculate number of hours between pass and now
$dayinpass = "2013-06-23 05:09:12";
$today = time();
$dayinpass= strtotime($dayinpass);
echo round(abs($today-$dayinpass)/60/60);
<?
$day1 = "2014-01-26 11:30:00";
$day1 = strtotime($day1);
$day2 = "2014-01-26 12:30:00";
$day2 = strtotime($day2);
$diffHours = round(($day2 - $day1) / 3600);
echo $diffHours;
?>
$day1 = "2006-04-12 12:30:00"
$day1 = strtotime($day1);
$day2 = "2006-04-14 11:30:00"
$day2 = strtotime($day2);
$diffHours = round(($day2 - $day1) / 3600);
I guess strtotime() function accept this date format.
Unfortunately the solution provided by FaileN doesn't work as stated by Walter Tross.. days may not be 24 hours!
I like to use the PHP Objects where possible and for a bit more flexibility I have come up with the following function:
/**
* #param DateTimeInterface $a
* #param DateTimeInterface $b
* #param bool $absolute Should the interval be forced to be positive?
* #param string $cap The greatest time unit to allow
*
* #return DateInterval The difference as a time only interval
*/
function time_diff(DateTimeInterface $a, DateTimeInterface $b, $absolute=false, $cap='H'){
// Get unix timestamps, note getTimeStamp() is limited
$b_raw = intval($b->format("U"));
$a_raw = intval($a->format("U"));
// Initial Interval properties
$h = 0;
$m = 0;
$invert = 0;
// Is interval negative?
if(!$absolute && $b_raw<$a_raw){
$invert = 1;
}
// Working diff, reduced as larger time units are calculated
$working = abs($b_raw-$a_raw);
// If capped at hours, calc and remove hours, cap at minutes
if($cap == 'H') {
$h = intval($working/3600);
$working -= $h * 3600;
$cap = 'M';
}
// If capped at minutes, calc and remove minutes
if($cap == 'M') {
$m = intval($working/60);
$working -= $m * 60;
}
// Seconds remain
$s = $working;
// Build interval and invert if necessary
$interval = new DateInterval('PT'.$h.'H'.$m.'M'.$s.'S');
$interval->invert=$invert;
return $interval;
}
This like date_diff() creates a DateTimeInterval, but with the highest unit as hours rather than years.. it can be formatted as usual.
$interval = time_diff($date_a, $date_b);
echo $interval->format('%r%H'); // For hours (with sign)
N.B. I have used format('U') instead of getTimestamp() because of the comment in the manual. Also note that 64-bit is required for post-epoch and pre-negative-epoch dates!
Carbon could also be a nice way to go.
From their website:
A simple PHP API extension for DateTime. http://carbon.nesbot.com/
Example:
use Carbon\Carbon;
//...
$day1 = Carbon::createFromFormat('Y-m-d H:i:s', '2006-04-12 12:30:00');
$day2 = Carbon::createFromFormat('Y-m-d H:i:s', '2006-04-14 11:30:00');
echo $day1->diffInHours($day2); // 47
//...
Carbon extends the DateTime class to inherit methods including diff(). It adds nice sugars like diffInHours, diffInMintutes, diffInSeconds e.t.c.
This function helps you to calculate exact years and months between two given dates, $doj1 and $doj. It returns example 4.3 means 4 years and 3 month.
<?php
function cal_exp($doj1)
{
$doj1=strtotime($doj1);
$doj=date("m/d/Y",$doj1); //till date or any given date
$now=date("m/d/Y");
//$b=strtotime($b1);
//echo $c=$b1-$a2;
//echo date("Y-m-d H:i:s",$c);
$year=date("Y");
//$chk_leap=is_leapyear($year);
//$year_diff=365.25;
$x=explode("/",$doj);
$y1=explode("/",$now);
$yy=$x[2];
$mm=$x[0];
$dd=$x[1];
$yy1=$y1[2];
$mm1=$y1[0];
$dd1=$y1[1];
$mn=0;
$mn1=0;
$ye=0;
if($mm1>$mm)
{
$mn=$mm1-$mm;
if($dd1<$dd)
{
$mn=$mn-1;
}
$ye=$yy1-$yy;
}
else if($mm1<$mm)
{
$mn=12-$mm;
//$mn=$mn;
if($mm!=1)
{
$mn1=$mm1-1;
}
$mn+=$mn1;
if($dd1>$dd)
{
$mn+=1;
}
$yy=$yy+1;
$ye=$yy1-$yy;
}
else
{
$ye=$yy1-$yy;
$ye=$ye-1;
$mn=12-1;
if($dd1>$dd)
{
$ye+=1;
$mn=0;
}
}
$to=$ye." year and ".$mn." months";
return $ye.".".$mn;
/*return daysDiff($x[2],$x[0],$x[1]);
$days=dateDiff("/",$now,$doj)/$year_diff;
$days_exp=explode(".",$days);
return $years_exp=$days; //number of years exp*/
}
?>
In addition to #fyrye's very helpful answer this is an okayish workaround for the mentioned bug (this one), that DatePeriod substracts one hour when entering summertime, but doesn't add one hour when leaving summertime (and thus Europe/Berlin's March has its correct 743 hours but October has 744 instead of 745 hours):
Counting the hours of a month (or any timespan), considering DST-transitions in both directions
function getMonthHours(string $year, string $month, \DateTimeZone $timezone): int
{
// or whatever start and end \DateTimeInterface objects you like
$start = new \DateTimeImmutable($year . '-' . $month . '-01 00:00:00', $timezone);
$end = new \DateTimeImmutable((new \DateTimeImmutable($year . '-' . $month . '-01 23:59:59', $timezone))->format('Y-m-t H:i:s'), $timezone);
// count the hours just utilizing \DatePeriod, \DateInterval and iterator_count, hell yeah!
$hours = iterator_count(new \DatePeriod($start, new \DateInterval('PT1H'), $end));
// find transitions and check, if there is one that leads to a positive offset
// that isn't added by \DatePeriod
// this is the workaround for https://bugs.php.net/bug.php?id=75685
$transitions = $timezone->getTransitions((int)$start->format('U'), (int)$end->format('U'));
if (2 === count($transitions) && $transitions[0]['offset'] - $transitions[1]['offset'] > 0) {
$hours += (round(($transitions[0]['offset'] - $transitions[1]['offset'])/3600));
}
return $hours;
}
$myTimezoneWithDST = new \DateTimeZone('Europe/Berlin');
var_dump(getMonthHours('2020', '01', $myTimezoneWithDST)); // 744
var_dump(getMonthHours('2020', '03', $myTimezoneWithDST)); // 743
var_dump(getMonthHours('2020', '10', $myTimezoneWithDST)); // 745, finally!
$myTimezoneWithoutDST = new \DateTimeZone('UTC');
var_dump(getMonthHours('2020', '01', $myTimezoneWithoutDST)); // 744
var_dump(getMonthHours('2020', '03', $myTimezoneWithoutDST)); // 744
var_dump(getMonthHours('2020', '10', $myTimezoneWithoutDST)); // 744
P.S. If you check a (longer) timespan, which leads to more than those two transitions, my workaround won't touch the counted hours to reduce the potential of funny side effects. In such cases, a more complicated solution must be implemented. One could iterate over all found transitions and compare the current with the last and check if it is one with DST true->false.
$diff_min = ( strtotime( $day2 ) - strtotime( $day1 ) ) / 60 / 60;
$total_time = $diff_min;
You can try this one.
// Create two new DateTime-objects...
$date1 = new DateTime('2006-04-12T12:30:00');
$date2 = new DateTime('2006-04-14T11:30:00');
// The diff-method returns difference in days...
$diffInDays = $date2->diffInDays($date1);
// The diff-method returns difference in hours...
$diffInHours = $date2->diffInHours($date1);
// The diff-method returns difference in mintes...
$diffInMinutes = $date2->diffInMinutes($date1);
The second part of the answer from #fidi doesn't factor in months/years.
$date1 = new DateTime('2006-04-12T12:30:00');
$date2 = new DateTime('2010-04-14T11:30:00');
$diff = $date2->diff($date1);
$hours = $diff->h;
$days = intval($diff->format('%a'));
$hours = $hours + ($days*24);
echo $hours;
This is working in my project. I think, This will be helpful for you.
If Date is in past then invert will 1.
If Date is in future then invert will 0.
$defaultDate = date('Y-m-d');
$datetime1 = new DateTime('2013-03-10');
$datetime2 = new DateTime($defaultDate);
$interval = $datetime1->diff($datetime2);
$days = $interval->format('%a');
$invert = $interval->invert;
To pass a unix timestamp use this notation
$now = time();
$now = new DateTime("#$now");
Related
Add fractional hours to a DateTime in php
I have a system which I need to add a certain amount of fractional hours. I've been searching and this is what I got, by far it's the most accurate method, but still doesn't give me the answer I need function calculateHours($hours){ $now = new DateTime("2017-10-25 10:23:00"); $time = array(); $time = explode(".", $hours); $time [1] += $time [0]*60; $now->modify("+".$time[1]." hours"); return $now; } $diff = 119.23; $answer = calculateHours($diff); echo $answer ->format('Y-m-d H:i:s'); The answer that I want to reach is "2017-11-09 11:00:00" and I receive "2017-10-25 12:22:23" instead
Adding the hours is not correct. When you multiply hours times 60 it will make minutes. This code should work. function calculateHours($hours){ $now = new DateTime("2017-10-25 10:23:00"); $time = explode(".", $hours); $time[1] += $time[0]*60; $now->modify("+".$time[1]." minutes"); return $now; } $diff = 119.23; $answer = calculateHours($diff); echo $answer->format('Y-m-d H:i:s'); Result is 2017-10-30 09:46:00
You should use DateInterval php class to create an inverval with x seconds from your $hours variable. Then you just have to use the datetime add interval method to modify your date Please take a look a this example function calculateHours($hours){ $now = new DateTime("2017-10-25 10:23:00"); var_dump($now); $timeParts = explode(".", $hours); // Where 23 is a percentage of on hour $minutes = $timeParts[0] * 60 + round($time[1] * 60 / 100); // Where 23 is the number of minutes $minutes = $timeParts[0] * 60 + $time[1]; $interval = new DateInterval(sprintf('PT%dM', $minutes)); $now->add($interval); echo $now->format('Y-m-d H:i:s'); return $now; }
Use date_add date_add($now, date_interval_create_from_date_string($tempo[1]' hours')); or as object: $now->add( DateInterval::createFromDateString($tempo[1].' hours'));
PHP DateInterval not return days in hours [duplicate]
How do I calculate the difference between two dates in hours? For example: day1=2006-04-12 12:30:00 day2=2006-04-14 11:30:00 In this case the result should be 47 hours.
The newer PHP-Versions provide some new classes called DateTime, DateInterval, DateTimeZone and DatePeriod. The cool thing about this classes is, that it considers different timezones, leap years, leap seconds, summertime, etc. And on top of that it's very easy to use. Here's what you want with the help of this objects: // 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('%a Day and %h hours'); The DateInterval-object, which is returned also provides other methods than format. If you want the result in hours only, you could to something like this: $date1 = new DateTime('2006-04-12T12:30:00'); $date2 = new DateTime('2006-04-14T11:30:00'); $diff = $date2->diff($date1); $hours = $diff->h; $hours = $hours + ($diff->days*24); echo $hours; And here are the links for documentation: DateTime-Class DateTimeZone-Class DateInterval-Class DatePeriod-Class All these classes also offer a procedural/functional way to operate with dates. Therefore take a look at the overview: http://php.net/manual/book.datetime.php
$t1 = strtotime( '2006-04-14 11:30:00' ); $t2 = strtotime( '2006-04-12 12:30:00' ); $diff = $t1 - $t2; $hours = $diff / ( 60 * 60 );
To provide another method for DatePeriod when using the UTC or GMT timezone. Count Hours https://3v4l.org/Mu3HD $start = new \DateTime('2006-04-12T12:30:00'); $end = new \DateTime('2006-04-14T11:30:00'); //determine what interval should be used - can change to weeks, months, etc $interval = new \DateInterval('PT1H'); //create periods every hour between the two dates $periods = new \DatePeriod($start, $interval, $end); //count the number of objects within the periods $hours = iterator_count($periods); echo $hours . ' hours'; //difference between Unix Epoch $diff = $end->getTimestamp() - $start->getTimestamp(); $hours = $diff / ( 60 * 60 ); echo $hours . ' hours (60 * 60)'; //difference between days $diff = $end->diff($start); $hours = $diff->h + ($diff->days * 24); echo $hours . ' hours (days * 24)'; Result 47 hours (iterator_count) 47 hours (60 * 60) 47 hours (days * 24) Count Hours with Daylight Savings https://3v4l.org/QBQUB Please be advised that DatePeriod excludes an hour for DST but does not add another hour when DST ends. So its usage is subjective to your desired outcome and date range. See the current bug report //set timezone to UTC to disregard daylight savings date_default_timezone_set('America/New_York'); $interval = new \DateInterval('PT1H'); //DST starts Apr. 2nd 02:00 and moves to 03:00 $start = new \DateTime('2006-04-01T12:00:00'); $end = new \DateTime('2006-04-02T12:00:00'); $periods = new \DatePeriod($start, $interval, $end); $hours = iterator_count($periods); echo $hours . ' hours'; //DST ends Oct. 29th 02:00 and moves to 01:00 $start = new \DateTime('2006-10-28T12:00:00'); $end = new \DateTime('2006-10-29T12:00:00'); $periods = new \DatePeriod($start, $interval, $end); $hours = iterator_count($periods); echo $hours . ' hours'; Result #2006-04-01 12:00 EST to 2006-04-02 12:00 EDT 23 hours (iterator_count) //23 hours (60 * 60) //24 hours (days * 24) #2006-10-28 12:00 EDT to 2006-10-29 12:00 EST 24 hours (iterator_count) //25 hours (60 * 60) //24 hours (days * 24) #2006-01-01 12:00 EST to 2007-01-01 12:00 EST 8759 hours (iterator_count) //8760 hours (60 * 60) //8760 hours (days * 24) //------ #2006-04-01 12:00 UTC to 2006-04-02 12:00 UTC 24 hours (iterator_count) //24 hours (60 * 60) //24 hours (days * 24) #2006-10-28 12:00 UTC to 2006-10-29 12:00 UTC 24 hours (iterator_count) //24 hours (60 * 60) //24 hours (days * 24) #2006-01-01 12:00 UTC to 2007-01-01 12:00 UTC 8760 hours (iterator_count) //8760 hours (60 * 60) //8760 hours (days * 24)
your answer is: round((strtotime($day2) - strtotime($day1))/(60*60))
The easiest way to get the correct number of hours between two dates (datetimes), even across daylight saving time changes, is to use the difference in Unix timestamps. Unix timestamps are seconds elapsed since 1970-01-01T00:00:00 UTC, ignoring leap seconds (this is OK because you probably don't need this precision, and because it's quite difficult to take leap seconds into account). The most flexible way to convert a datetime string with optional timezone information into a Unix timestamp is to construct a DateTime object (optionally with a DateTimeZone as a second argument in the constructor), and then call its getTimestamp method. $str1 = '2006-04-12 12:30:00'; $str2 = '2006-04-14 11:30:00'; $tz1 = new DateTimeZone('Pacific/Apia'); $tz2 = $tz1; $d1 = new DateTime($str1, $tz1); // tz is optional, $d2 = new DateTime($str2, $tz2); // and ignored if str contains tz offset $delta_h = ($d2->getTimestamp() - $d1->getTimestamp()) / 3600; if ($rounded_result) { $delta_h = round ($delta_h); } else if ($truncated_result) { $delta_h = intval($delta_h); } echo "Δh: $delta_h\n";
//Calculate number of hours between pass and now $dayinpass = "2013-06-23 05:09:12"; $today = time(); $dayinpass= strtotime($dayinpass); echo round(abs($today-$dayinpass)/60/60);
<? $day1 = "2014-01-26 11:30:00"; $day1 = strtotime($day1); $day2 = "2014-01-26 12:30:00"; $day2 = strtotime($day2); $diffHours = round(($day2 - $day1) / 3600); echo $diffHours; ?>
$day1 = "2006-04-12 12:30:00" $day1 = strtotime($day1); $day2 = "2006-04-14 11:30:00" $day2 = strtotime($day2); $diffHours = round(($day2 - $day1) / 3600); I guess strtotime() function accept this date format.
Unfortunately the solution provided by FaileN doesn't work as stated by Walter Tross.. days may not be 24 hours! I like to use the PHP Objects where possible and for a bit more flexibility I have come up with the following function: /** * #param DateTimeInterface $a * #param DateTimeInterface $b * #param bool $absolute Should the interval be forced to be positive? * #param string $cap The greatest time unit to allow * * #return DateInterval The difference as a time only interval */ function time_diff(DateTimeInterface $a, DateTimeInterface $b, $absolute=false, $cap='H'){ // Get unix timestamps, note getTimeStamp() is limited $b_raw = intval($b->format("U")); $a_raw = intval($a->format("U")); // Initial Interval properties $h = 0; $m = 0; $invert = 0; // Is interval negative? if(!$absolute && $b_raw<$a_raw){ $invert = 1; } // Working diff, reduced as larger time units are calculated $working = abs($b_raw-$a_raw); // If capped at hours, calc and remove hours, cap at minutes if($cap == 'H') { $h = intval($working/3600); $working -= $h * 3600; $cap = 'M'; } // If capped at minutes, calc and remove minutes if($cap == 'M') { $m = intval($working/60); $working -= $m * 60; } // Seconds remain $s = $working; // Build interval and invert if necessary $interval = new DateInterval('PT'.$h.'H'.$m.'M'.$s.'S'); $interval->invert=$invert; return $interval; } This like date_diff() creates a DateTimeInterval, but with the highest unit as hours rather than years.. it can be formatted as usual. $interval = time_diff($date_a, $date_b); echo $interval->format('%r%H'); // For hours (with sign) N.B. I have used format('U') instead of getTimestamp() because of the comment in the manual. Also note that 64-bit is required for post-epoch and pre-negative-epoch dates!
Carbon could also be a nice way to go. From their website: A simple PHP API extension for DateTime. http://carbon.nesbot.com/ Example: use Carbon\Carbon; //... $day1 = Carbon::createFromFormat('Y-m-d H:i:s', '2006-04-12 12:30:00'); $day2 = Carbon::createFromFormat('Y-m-d H:i:s', '2006-04-14 11:30:00'); echo $day1->diffInHours($day2); // 47 //... Carbon extends the DateTime class to inherit methods including diff(). It adds nice sugars like diffInHours, diffInMintutes, diffInSeconds e.t.c.
This function helps you to calculate exact years and months between two given dates, $doj1 and $doj. It returns example 4.3 means 4 years and 3 month. <?php function cal_exp($doj1) { $doj1=strtotime($doj1); $doj=date("m/d/Y",$doj1); //till date or any given date $now=date("m/d/Y"); //$b=strtotime($b1); //echo $c=$b1-$a2; //echo date("Y-m-d H:i:s",$c); $year=date("Y"); //$chk_leap=is_leapyear($year); //$year_diff=365.25; $x=explode("/",$doj); $y1=explode("/",$now); $yy=$x[2]; $mm=$x[0]; $dd=$x[1]; $yy1=$y1[2]; $mm1=$y1[0]; $dd1=$y1[1]; $mn=0; $mn1=0; $ye=0; if($mm1>$mm) { $mn=$mm1-$mm; if($dd1<$dd) { $mn=$mn-1; } $ye=$yy1-$yy; } else if($mm1<$mm) { $mn=12-$mm; //$mn=$mn; if($mm!=1) { $mn1=$mm1-1; } $mn+=$mn1; if($dd1>$dd) { $mn+=1; } $yy=$yy+1; $ye=$yy1-$yy; } else { $ye=$yy1-$yy; $ye=$ye-1; $mn=12-1; if($dd1>$dd) { $ye+=1; $mn=0; } } $to=$ye." year and ".$mn." months"; return $ye.".".$mn; /*return daysDiff($x[2],$x[0],$x[1]); $days=dateDiff("/",$now,$doj)/$year_diff; $days_exp=explode(".",$days); return $years_exp=$days; //number of years exp*/ } ?>
In addition to #fyrye's very helpful answer this is an okayish workaround for the mentioned bug (this one), that DatePeriod substracts one hour when entering summertime, but doesn't add one hour when leaving summertime (and thus Europe/Berlin's March has its correct 743 hours but October has 744 instead of 745 hours): Counting the hours of a month (or any timespan), considering DST-transitions in both directions function getMonthHours(string $year, string $month, \DateTimeZone $timezone): int { // or whatever start and end \DateTimeInterface objects you like $start = new \DateTimeImmutable($year . '-' . $month . '-01 00:00:00', $timezone); $end = new \DateTimeImmutable((new \DateTimeImmutable($year . '-' . $month . '-01 23:59:59', $timezone))->format('Y-m-t H:i:s'), $timezone); // count the hours just utilizing \DatePeriod, \DateInterval and iterator_count, hell yeah! $hours = iterator_count(new \DatePeriod($start, new \DateInterval('PT1H'), $end)); // find transitions and check, if there is one that leads to a positive offset // that isn't added by \DatePeriod // this is the workaround for https://bugs.php.net/bug.php?id=75685 $transitions = $timezone->getTransitions((int)$start->format('U'), (int)$end->format('U')); if (2 === count($transitions) && $transitions[0]['offset'] - $transitions[1]['offset'] > 0) { $hours += (round(($transitions[0]['offset'] - $transitions[1]['offset'])/3600)); } return $hours; } $myTimezoneWithDST = new \DateTimeZone('Europe/Berlin'); var_dump(getMonthHours('2020', '01', $myTimezoneWithDST)); // 744 var_dump(getMonthHours('2020', '03', $myTimezoneWithDST)); // 743 var_dump(getMonthHours('2020', '10', $myTimezoneWithDST)); // 745, finally! $myTimezoneWithoutDST = new \DateTimeZone('UTC'); var_dump(getMonthHours('2020', '01', $myTimezoneWithoutDST)); // 744 var_dump(getMonthHours('2020', '03', $myTimezoneWithoutDST)); // 744 var_dump(getMonthHours('2020', '10', $myTimezoneWithoutDST)); // 744 P.S. If you check a (longer) timespan, which leads to more than those two transitions, my workaround won't touch the counted hours to reduce the potential of funny side effects. In such cases, a more complicated solution must be implemented. One could iterate over all found transitions and compare the current with the last and check if it is one with DST true->false.
$diff_min = ( strtotime( $day2 ) - strtotime( $day1 ) ) / 60 / 60; $total_time = $diff_min; You can try this one.
// Create two new DateTime-objects... $date1 = new DateTime('2006-04-12T12:30:00'); $date2 = new DateTime('2006-04-14T11:30:00'); // The diff-method returns difference in days... $diffInDays = $date2->diffInDays($date1); // The diff-method returns difference in hours... $diffInHours = $date2->diffInHours($date1); // The diff-method returns difference in mintes... $diffInMinutes = $date2->diffInMinutes($date1);
The second part of the answer from #fidi doesn't factor in months/years. $date1 = new DateTime('2006-04-12T12:30:00'); $date2 = new DateTime('2010-04-14T11:30:00'); $diff = $date2->diff($date1); $hours = $diff->h; $days = intval($diff->format('%a')); $hours = $hours + ($days*24); echo $hours;
This is working in my project. I think, This will be helpful for you. If Date is in past then invert will 1. If Date is in future then invert will 0. $defaultDate = date('Y-m-d'); $datetime1 = new DateTime('2013-03-10'); $datetime2 = new DateTime($defaultDate); $interval = $datetime1->diff($datetime2); $days = $interval->format('%a'); $invert = $interval->invert;
To pass a unix timestamp use this notation $now = time(); $now = new DateTime("#$now");
PHP - Calculating working hours between two dates but exclude the time when request is on hold
Can some one help me write a function that calculates the number of working hours between two dates but want to exclude the time when the request had a status of "On Hold". So lets say the request came in at 3PM friday and was closed at 3PM Wednesday, and working hours are from 8AM to 5PM pacific (Mon thru Friday)...Total working hours will be 27 hours...but if the request remained on hold from Monday 3PM till Tuesday 3PM...Actual work time on the request really becomes 18 hours instead of 27 hours. I have recently started working on PHP and have been assigned this task which is very confusing to me. Please help
All you have to do is get the total time elapsed, then substract the non-working hours. You can use dateTime and datePeriod php objects for that (requires php 5.3) Here a small script to do what you want (but you will have probably to adapt for your needs) <?php ini_set('display_errors', 'on'); define('DAY_WORK', 32400); // 9 * 60 * 60 define('HOUR_START_DAY', '08:00:00'); define('HOUR_END_DAY', '17:00:00'); // get begin and end dates of the full period $date_begin = '2013-11-29 15:00:00'; $date_end = '2013-12-03 15:00:00'; // keep the initial dates for later use $d1 = new DateTime($date_begin); $d2 = new DateTime($date_end); // and get the datePeriod from the 1st to the last day $period_start = new DateTime($d1->format('Y-m-d 00:00:00')); $period_end = new DateTime($d2->format('Y-m-d 23:59:59')); $interval = new DateInterval('P1D'); //$interval = new DateInterval('weekdays'); // 1 day interval to get all days between the period $period = new DatePeriod($period_start, $interval, $period_end); $worked_time = 0; $nb = 0; // for every worked day, add the hours you want foreach($period as $date){ $week_day = $date->format('w'); // 0 (for Sunday) through 6 (for Saturday) if (!in_array($week_day,array(0, 6))) { // if this is the first day or the last dy, you have to count only the worked hours if ($date->format('Y-m-d') == $d1->format('Y-m-d')) { $end_of_day_format = $date->format('Y-m-d '.HOUR_END_DAY); $d1_format = $d1->format('Y-m-d H:i:s'); $end_of_day = new DateTime($end_of_day_format); $diff = $end_of_day->diff($d1)->format("%H:%I:%S"); $diff = split(':', $diff); $diff = $diff[0]*3600 + $diff[1]*60 + $diff[0]; $worked_time += $diff; } else if ($date->format('Y-m-d') == $d2->format('Y-m-d')) { $start_of_day = new DateTime($date->format('Y-m-d '.HOUR_START_DAY)); $d2_format = $d2->format('Y-m-d H:i:s'); $end_of_day = new DateTime($end_of_day_format); $diff = $start_of_day->diff($d2)->format('%H:%I:%S'); $diff = split(':', $diff); $diff = $diff[0]*3600 + $diff[1]*60 + $diff[0]; $worked_time += $diff; } else { // otherwise, just count the full day of work $worked_time += DAY_WORK; } } if ($nb> 10) die("die ".$nb); } echo sprintf('Works from %s to %s, You worked %d hour(s)', $date_begin, $date_end, $worked_time/60/60);
Calculate work time with an accuracy of 1 minute. WARNING: This function can take many seconds to load as it does a loop for every minute between the time span. <?php $request = array( 'start' => '3PM Nov 29 2013', 'end' => '3PM Dec 4 2013' ); echo calculate_work($request); /** * Calculate work time by looping through every minute * #param array $request start to end time * #return int work time in minutes */ function calculate_work($request) { $start = strtotime($request['start']); $end = strtotime($request['end']); $work_time = 0; /* Add 1 minute to the start so that we don't count 0 as a minute */ for ($time = $start + 60; $time <= $end; $time += 60) { // Weekends if (date('D', $time) == 'Sat' OR date('D', $time) == 'Sun') continue; // Non Working Hours if (date('Hi', $time) <= '0800' OR date('Hi', $time) > '1700') continue; // On Hold if ($time > strtotime('3PM Dec 2 2013') AND $time <= strtotime('3PM Dec 3 2013')) continue; $work_time++; } // Divide by 60 to turn minutes into hours return $work_time / 60; }
/** * Get the total working hours in seconds between 2 dates.. * #param DateTime $start Start Date and Time * #param DateTime $end Finish Date and Time * #param array $working_hours office hours for each weekday (0 Monday, 6 Sunday), Each day must be an array containing a start/finish time in seconds since midnight. * #return integer * #link https://github.com/RCrowt/working-hours-calculator */ function getWorkingHoursInSeconds(DateTime $start, DateTime $end, array $working_hours) { $seconds = 0; // Total working seconds // Calculate the Start Date (Midnight) and Time (Seconds into day) as Integers. $start_date = clone $start; $start_date = $start_date->setTime(0, 0, 0)->getTimestamp(); $start_time = $start->getTimestamp() - $start_date; // Calculate the Finish Date (Midnight) and Time (Seconds into day) as Integers. $end_date = clone $end; $end_date = $end_date->setTime(0, 0, 0)->getTimestamp(); $end_time = $end->getTimestamp() - $end_date; // For each Day for ($today = $start_date; $today <= $end_date; $today += 86400) { // Get the current Weekday. $today_weekday = date('w', $today); // Skip to next day if no hours set for weekday. if (!isset($working_hours[$today_weekday][0]) || !isset($working_hours[$today_weekday][1])) continue; // Set the office hours start/finish. $today_start = $working_hours[$today_weekday][0]; $today_end = $working_hours[$today_weekday][1]; // Adjust Start/Finish times on Start/Finish Day. if ($today === $start_date) $today_start = min($today_end, max($today_start, $start_time)); if ($today === $end_date) $today_end = max($today_start, min($today_end, $end_time)); // Add to total seconds. $seconds += $today_end - $today_start; } return gmdate("H:i:s", $seconds); }
How to get the hour minute and second?
The future time is :2012-05-26 00:00:00 supposed there are three variable: $hour $minute $second now, i want to using the future time subtract now time. then give the left hour to $hour,give the left minute to $minute,give the left second to $second. i am sorry i am new of php, now i get stucked how to do the math operation ? thank you
A very good resource for dates and time.. http://www.php.net/manual/en/function.time.php -there are samples here doing something similar.
Check the date_diff function. There's the exact solution to what you're asking there. And here's the page (DateInterval::format) documenting how you can format the output. $now = date_create(); // use "now" and necessary DateTimeZone in the arguments $otherDate = date_create('2020-04-13'); $interval = date_diff($now, $futureDate); echo $interval->format('%a days');
The following are the math operations for the difference in hours,minutes and seconds $future_datetime = '2012-05-26 00:00:00'; $future = strtotime($future_datetime); //future datetime in seconds $now_datetime = date('Y-m-d H:i:s'); $now = date('U'); //now datetime in seconds //The math for calculating the difference in hours, minutes and seconds $difference = $future - $now; $second = 1; $minute = 60 * $second; $hour = 60 * $minute; $difference_hours = floor($difference/$hour); $remainder = $difference - ($difference_hours * $hour); $difference_minutes = floor($remainder/$minute); $remainder = $remainder - ($difference_minutes * $minute); $difference_seconds = $remainder; echo "The difference between $future_datetime and $now_datetime is $difference_hours hours, $difference_minutes minutes and $difference_seconds seconds";
How do I find the hour difference between two dates in PHP?
I have two dates, formated like "Y-m-d H:i:s". I need to compare these two dates and figure out the hour difference.
You can convert them to timestamps and go from there: $hourdiff = round((strtotime($time1) - strtotime($time2))/3600, 1); Dividing by 3600 because there are 3600 seconds in one hour and using round() to avoid having a lot of decimal places.
You can use DateTime class also - $d1= new DateTime("06-08-2015 01:33:26pm"); // first date $d2= new DateTime("06-07-2015 10:33:26am"); // second date $interval= $d1->diff($d2); // get difference between two dates echo ($interval->days * 24) + $interval->h; // convert days to hours and add hours from difference
As an addition to accepted answer I would like to remind that \DateTime::diff is available! $f = 'Y-m-d H:i:s'; $d1 = \DateTime::createFromFormat($date1, $f); $d2 = \DateTime::createFromFormat($date2, $f); /** * #var \DateInterval $diff */ $diff = $d2->diff($d1); $hours = $diff->h + ($diff->days * 24); // + ($diff->m > 30 ? 1 : 0) to be more precise \DateInterval documentation.
$seconds = strtotime($date2) - strtotime($date1); $hours = $seconds / 60 / 60;
You can try this : $time1 = new DateTime('06:56:58'); $time2 = new DateTime('15:35:00'); $time_diff = $time1->diff($time2); echo $time_diff->h.' hours'; echo $time_diff->i.' minutes'; echo $time_diff->s.' seconds'; Output: 8 hours 38 minutes 2 seconds
The problem is that using these values the result is 167 and it should be 168: $date1 = "2014-03-07 05:49:23"; $date2 = "2014-03-14 05:49:23"; $seconds = strtotime($date2) - strtotime($date1); $hours = $seconds / 60 / 60;
$date1 = date_create('2016-12-12 09:00:00'); $date2 = date_create('2016-12-12 11:00:00'); $diff = date_diff($date1,$date2); $hour = $diff->h;
This is because of day time saving. Daylight Saving Time (United States) 2014 began at 2:00 AM on Sunday, March 9. You lose one hour during the period from $date1 = "2014-03-07 05:49:23" to $date2 = "2014-03-14 05:49:23";
You can try this: $dayinpass = "2016-09-23 20:09:12"; $today = time(); $dayinpass= strtotime($dayinpass); echo round(abs($today-$dayinpass)/60/60);
You can use strtotime() to parse your strings and do the difference between the two of them. Resources : php.net - strtotime()