I have 2 DateTimes, I need to calculate the difference between them in hours in decimal format. The hard part is making sure the result is storing the value to 2 decimal places.
$datetime1 = new DateTime("2017-09-01 23:00:00");
$datetime2 = new DateTime();
$difference = $datetime2->diff($datetime1);
But the result is just a whole number which loses too much accuracy for me. How to keep the value to 2 decimal places?
DateTime::diff returns DaterInterval that has a whole number for each individual time related property.
The days property does not account for increments less than a day, as they are accumulated to and removed from the lesser properties and then is rounded down.
So $diff->h will never be greater than 23. While $diff->s and $diff->i will never be greater than 59. Where days will contain the total days within the year and month properties. Not to be confused with $diff->d, which is the incremental number of days.
In order to determine the total hours using diff, you just need to perform math on each of the properties to retrieve the number of hours of the property.
Example: https://3v4l.org/KhBQC
$datetime1 = new DateTime('2017-09-01 23:00:00');
$datetime2 = new DateTime('2017-09-02 01:34:00');
$diff = $datetime2->diff($datetime1);
$hours = round($diff->s / 3600 + $diff->i / 60 + $diff->h + $diff->days * 24, 2);
echo $hours; //2.57
In php 7.1 you can also account for microseconds by adding $diff->f / 3.6e+9.
A more simplistic approach would be to subtract the unix timestamps, to retrieve the total number of seconds between the two dates. Then divide the remaining seconds by the number of seconds in an hour (3600).
Example: https://3v4l.org/SbjEU
$datetime1 = new DateTime('2017-09-01 23:00:00');
$datetime2 = new DateTime('2017-09-02 01:34:00');
$hours = round(($datetime2->getTimestamp() - $datetime1->getTimestamp()) / 3600, 2);
echo $hours; //2.57
2 decimal places:
$datetime1 = new DateTime("2017-09-01 23:00:00");
$datetime2 = new DateTime();
$epoch1 = $datetime1->getTimestamp();
$epoch2 = $datetime2->getTimestamp();
$diff = $epoch1 - $epoch2;
echo number_format( $diff / 3600, '2' );
Related
i try to make time difference with carbon
$dt = Carbon::parse('2018-07-15 00:00:00');
$now = Carbon::now('Asia/Dubai'); //set current time
$seconds = $now->diffInSeconds( $dt ); //difference turn into second
$days = $dt->diffInDays($dt->copy()->addSeconds($seconds));
$hours = $dt->diffInHours($dt->copy()->addSeconds($seconds)->subDays($days));
$minutes = $dt->diffInMinutes($dt->copy()->addSeconds($seconds)->subHours($hours));
$days result are 12 (its right).
$hours result are 8 (seems not right).
$minutes result are 17299 (clearly wrong).
how to get the result for example 12 day 5 hours 45 minutes
Actually functions like diffInSeconds give total difference in seconds that's why the number is so large,to get the minutes for the time difference right you can use -:
$minutes = ($now->minute - $dt->minute);
I have two Timestamps
2016-01-01 00:00:00
2016-01-02 23:59:59
Using PHP how can I calculate the number of hours and minutes between the two times and get the result as a decimal with 2 places after the .
currently I have this:
$Start = new DateTime($StartTime);
$Finish = new DateTime ($FinishTime);
$Interval = date_diff($Start,$Finish);
$Hours = $Interval->format('%h.%i');
But the result is incorrect if the user starts the timer on Day 1 and finishes on day 2.
You could multiply the number of days by 24 to convert them to hours, then sum the hours and concatenate the minutes:
$start = new DateTime('2016-01-01 00:00:00');
$end = new DateTime('2016-01-02 23:59:59');
$interval = $end->diff($start);
$days = $interval->format('%d');
$hours = 24 * $days + $interval->format('%h');
echo $hours.':'.$interval->format('%i');
You could format the DateTime as a UNIX timestamp, and then simply subtract to get the total seconds, and format the output with gmdate().
$Start = new DateTime($StartTime);
$Finish = new DateTime ($FinishTime);
$Interval = $Start->format('U') - $Finish->format('U');
$Hours = gmdate("H:i:s", $Interval);
Try this.
$Hours = $Interval->format('%a.%h.%i');
I have created a function to work out the amount of days and then weeks between 2 dates, in the example below there is 35 days resulting exactly 5 weeks, however the function is returning just short of this at 4.8571428571429 - the division is killing the remainder and coming out at 4.
I could use the ceil function to round it up to 5 but is this this a safe method for all dates? or is there a better way to do it?
$date1 = new DateTime('2015-02-23');
$date2 = new DateTime('2015-03-29');
$diff = $date2->diff($date1)->format("%a");
$weeks = $diff / 7;
return $weeks;
$date2->add(new DateInterval('P1D'));
Use this line after your $date2 initialisation.
Why will this work?
If you count, you have 34 days now: FROM 23-02 0:00 TILL 29-03 0:00.
If you want to count this last day also (to have 35 days), you'll need to add an extra day.
Add a single day to $date2
$date1 = new DateTime('2015-02-23');
$date2 = new DateTime('2015-03-29');
$date2->add(new DateInterval('P1D'));
$diff = $date2->diff($date1)->format("%a");
$weeks = $diff / 7;
return $weeks;
However, you still don't have a whole number of weeks difference with your original dates
I have to find total months between two dates in Unix timestamp formats. I want to create a PHP function for that. I've tried this:
get_total_month($startunixdate, $endunixdate) {
$monthdiff = $endunixdate-$startunixdate;
$monthdiff = $monthdiff / 60*60*24*31;
return $monthdiff;
}
Does this function consider leap years as well as month with 30 and 31 separately, or it will just count an approximate month?
Your answer is in this line;
$monthdiff = $monthdiff / 60*60*24*31
This will just count a month based on 31 days. The code divides the seconds by 60 to get the number of minutes, 60 again to get hours, 24 to get number of days, then it uses 31 as the length of a month.
This will result in an approximation of the number of months. For instance, if you take timestamps at the beginning and end of February (start of March), you will usually have a timestamp difference equivalent to 28 days (29 in a leap year). Dividing that by 31 will give you a fraction less than 1, since you are using 31 to represent a full month, when in reality a whole calendar month has passed.
In order to do this, use the DateTime class.
function get_total_month($start, $end) {
// Create DateTime objects
$dt1 = new DateTime($start);
$dt2 = new DateTime($end);
// Get DateInterval object representing difference between the two
$diff = $dt1->diff($dt2); // OR: $diff = date_diff($dt1, $dt2);
// Print the "months" out
echo $diff->format("Difference: %R%m months"); // OR access $diff->m manually
U can use PHP 5.3 datetime method.
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
Reference: PHP DateTime
I want to calculate the difference between two times, one of which is the current time, and the other is just in the format HH:MM, always in the future.
If I just subtract $futuretime from $now, it should, of course, be a positive number.
This works fine until...
If $now is in the afternoon or evening and $futuretime is, say, 7AM next morning, how can I force it to understand the the time is always going to be in the future?
(It's for working out the time of something that occurs about every half an hour during working hours, and then stops until the following morning)
Thanks in advance!
Simply add a whole day to the difference if it is negative. Assuming that $now and $futuretime are timestamps (stored in seconds since midnight), simply do the following:
$diff = $futuretime - $now;
if ($diff < 0)
$diff += 24 * 3600;
If they are in HH:MM format:
list($future_hours, $future_mins) = explode(":", $futuretime);
$futuretime = $future_hours * 60 + $future_mins;
list($now_hours, $now_mins) = explode(":", $now);
$now = $now_hours * 60 + $now_mins;
$diff = $futuretime - $now;
if ($diff < 0)
$diff += 24 * 60;
Of course the latter case returns the difference in minutes, while the former returns the difference in seconds.
Edit: as Andy noted below in the comments, this is not a good solution if you care about changes in DST. See his solution for a better approach.
If you're on PHP 5.3+, use PHP's DateTime:
$d1 = new DateTime('14:52:10');
$d2 = new DateTime('12:12:10');
$diff = $d1->diff( $d2 );
var_dump( $diff );
You can simply convert both dates to timestamp, do the calculations and convert it to required format, i.e. days, hours and minutes. it's quite simple.