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');
Related
Is there any build in function in PHP that can calculate the amount on minutes of AM and PM time in a date-time interval (working with 24h format). Lets say we have:
$start_date = '2021-03-30 11:30:00';
$end_date = '2021-03-30 12:30:00';
So it will output:
$AM = 30;
$PM = 30;
Another example:
$start_date = '2021-03-30 10:00:00';
$end_date = '2021-03-30 13:30:00';
Should output:
$AM = 120;
$PM = 90;
Create DateTime objects from those date strings and manually create a mid_noon variable datetime object. Find the difference of both objects with mid_noon using DateTime::diff and calculate the time of difference in minutes like below:
<?php
$start_date = DateTime::createFromFormat('Y-m-d H:i:s','2021-03-30 10:00:00');
$end_date = DateTime::createFromFormat('Y-m-d H:i:s','2021-03-30 13:30:00');
$mid_noon = DateTime::createFromFormat('Y-m-d H:i:s',$start_date->format('Y-m-d'). ' 12:00:00');
$start_diff = $mid_noon->diff($start_date);
$end_diff = $end_date->diff($mid_noon);
$am = $start_diff->h * 60 + $start_diff->i;
$pm = $end_diff->h * 60 + $end_diff->i;
echo $am," ",$pm;
Solution with strtotime and some elementary school math.
$am = intdiv(43200 - strtotime('2021-03-30 10:00:00')%86400,60);
$pm = intdiv(strtotime('2021-03-30 13:30:00')%86400 - 43200,60);
strtotime provides the seconds since 01/01/1970. With seconds%86400 we get the seconds from midnight. 86400 are the seconds of a whole day. 43200 is the seconds of half a day or 12:00. The differences 43200 - secondsStart and secondsEnd - 43200 give us the seconds for AM and PM. We only have to divide this by 60 to get the minutes.
The following variant with the DateTime extension dt is easier to understand.
Given:
$start_date = '2021-03-30 10:00:00';
$end_date = '2021-03-30 13:30:00';
Calculation:
$day12h = dt::create($start_date)->setTime('12:00');
$am = dt::create($start_date)->diffTotal($day12h,'Minutes');
$pm = $day12h->diffTotal($end_date,'Minutes');
I want to calculate the time difference from now (lets say 18:30:00) till this evening at 20pm.
$today = date('Y-m-d', time());
$remain = strtotime($today. " 00:00:00 + 20 hours") - time();
$remain = date('H:i:s', $remain);
I get a result which is one hour larger (02:30:00) than the actual result (01:30:00). I tried setting time zones but it's always the same result.
Using the DateTime object, you can do this easily:
$d1 = new DateTime('2015-04-23 18:30');
$d2 = new DateTime('2015-04-23 20:00');
$interval = $d2->diff($d1);
echo $interval->format('%H:%i hours');
Can you help me with this?
I am creating an event planner and I have a function that consist of hour range. For example I have an event BIRTHDAY and this event is started from 13:15:00 hours to 18:30:00 hours. Now how can I count the hours inside this range?
I have a code like this:
<?php
$start = "13:15:00";
$end = "18:30:00";
$time = strtotime($start);
$timeStop = strtotime($end);
//how can I count the hours inside?
?>
You could use gmdate() in this case:
$time = gmdate('H:i:s', $timeStop - $time); // feed seconds
echo $time;
Or with DateTime class also:
$time = new DateTime($start);
$timeStop = new DateTime($end);
$diff = $timeStop->diff($time);
echo $diff->format('%h:%i'); // hours minutes
<?php
$start = "13:15:00";
$end = "18:30:00";
$time = strtotime($start);
$timeStop = strtotime($end);
//To count hours in the range
$diff = intval(($timeStop - $time)/3600);
//3600 refers to 1 hour
?>
So you take the difference between two timestamps and divide by 3600 seconds to get the number of hours. intval is to get an integer value. You are free to use round, ceil or floor.
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 to get duration between start_date and end_date in hrs min sec format in php?
$start_date=2012-03-23 11:58:14 and $end_date=2012-03-24 11:54:29
Use DateTime class:
$start_date = new DateTime('2012-03-23 11:58:14');
$end_date = new DateTime('2012-03-24 11:54:29');
$dd = date_diff($end_date, $start_date);
To get hours use $dd->h, minutes - $dd->i, seconds - $dd->s.
echo "Hours = $dd->h, Minutes = $dd->i, Seconds = $dd->s";
I would
$start_time = strtotime($start_date);
$end_time = strtotime($end_date);
$difference = $end_time - $start_time;
echo date('H:i:s', $difference);
EDIT
I made an error in assuming that the time difference would be less then a day, so if the time difference is greater then a day, you will only see the Hours:minuetes:seconds, which is probably not what you want (if it is ignore this)
So I would now
$seconds = $difference % 60; //seconds
$difference = floor($difference / 60);
$min = $difference % 60; // min
$difference = floor($difference / 60);
$hours = $difference; //hours
echo "$hours : $min : $seconds";
Sorry for the correction
This function will help you
function datediff($interval, $datefrom, $dateto, $using_timestamps = false) {
/*
$interval can be:
yyyy - Number of full years
q - Number of full quarters
m - Number of full months
y - Difference between day numbers
(eg 1st Jan 2004 is "1", the first day. 2nd Feb 2003 is "33". The datediff is "-32".)
d - Number of full days
w - Number of full weekdays
ww - Number of full weeks
h - Number of full hours
n - Number of full minutes
s - Number of full seconds (default)
*/
http://php.net/manual/en/function.date-diff.php
format those string to date
transform those date to milliseconds
do endate - stardate
from the result calculate the h:mm:ss
See it working here: http://codepad.viper-7.com/FPuOks
$start_date="2012-03-22 11:58:14";
$end_date="2012-03-24 11:54:29";
$start_time = strtotime($start_date);
$end_time = strtotime($end_date);
$difference = $end_time - $start_time;
echo sprintf("%02d%s%02d%s%02d", floor($difference/3600), ':', ($difference/60)%60, ':', $difference%60); // outputs 47:56:15