This question already has answers here:
How to get millisecond between two dateTime obj?
(5 answers)
Closed 3 years ago.
How to get a range between two dates in a millisecond :
I have an example of the correct answer:
time_start = 2018-12-18 12:33:53.1231;
time_end = 2018-12-18 12:34:10.1523;
result = 17.0292;
what I'm asking is, what is the formula for calculating it?
I have tried some code but the result is 16.708
$dt_start = 2018-12-18 12:33:53.1231;
$dt_end = 2018-12-18 12:34:10.1523;
$ex_start = explode(".",$dt_start);
$ex_end = explode(".",$dt_end);
$datetime_start = strtotime($ex_start[0]);
$datetime_end = strtotime($ex_end[0]);
$total_detik = $datetime_end-$datetime_start;
// dd($ex_end[0]);
$milis_start = floatval($ex_start[1]);
$milis_end = floatval($ex_end[1]);
$range_milis = $milis_start-$milis_end;
$new_range_milis = $range_milis/10000;
// dd($range_milis);
$second = $datetime_end-$datetime_start+$range_milis/10000;
dd($second);
//output '16.708'
Try this to find the difference between two time.
<?php
function calculateTransactionDuration($startDate, $endDate)
{
$startDateFormat = new DateTime($startDate);
$EndDateFormat = new DateTime($endDate);
$uDiff = ($startDateFormat->format('u') - $EndDateFormat->format('u')) / (1000 * 1000);;
$diff = $startDateFormat->diff($EndDateFormat);
$s = (int) $diff->format('%s') - $uDiff;
$i = (int) ($diff->format('%i')) * 60;
$h = (int) ($diff->format('%h')) * 60 * 60;
return sprintf('%.6f', abs($h + $i + $s));
}
$time_start = '2018-12-18 12:33:53.1231';
$time_end = '2018-12-18 12:34:10.1523';
echo $difference = calculateTransactionDuration($time_start, $time_end);
// output = 17.029200
Related
My code:
$one = new DateTime("2018-03-15 11:53:13");
$two = new DateTime("2018-03-15 13:53:00");
$diff = $one->diff($two);
$three = new DateTime("2018-03-15 11:52:55");
$four = new DateTime("2018-03-16 11:52:57");
$difftwo = $three->diff($four);
$day = $diff->format('%H:%I:%S');
$day2 = $difftwo->format('%H:%I:%S');
$secs = strtotime($day2)-strtotime("00:00:00");
$result = date("H:i:s",strtotime($day) + $secs);
echo $result;
- $day = 01:59:47
- $day2 = 00:00:02 and 1 day
Result : 01:59:49
But I want to show : 1 01:59:49 (1 is a day result of $day2)
Can someone help me find the solution?
You could create 2 new same date. In one of them, add your 2 intervals.
Then you could use the DateInterval object to get your value:
$one = new DateTime('2018-03-15 11:53:13');
$two = new DateTime('2018-03-15 13:53:00');
$diff = $one->diff($two);
$three = new DateTime('2018-03-15 11:52:55');
$four = new DateTime('2018-03-16 11:52:57');
$difftwo = $three->diff($four);
$d1 = new DateTime(); // Now
$d2 = new DateTime(); // Now
$d1->add($diff); // Add 1st interval
$d1->add($difftwo); // Add 2nd interval
// diff between d2 and d1 gives total interval
echo $d2->diff($d1)->format('%d %H:%I:%S') ;
Outputs :
1 01:59:49
This question already has answers here:
How to get time difference in minutes in PHP
(21 answers)
Closed 5 years ago.
I have to find the total working time of a staff.
Variables in my hand is,
$work_time_start = "8:00:00";
$work_time_end = "18:00:00";
$lunch_break_start = "13:00:00";
$lunch_break_end = "14:00:00";
From this I have to find the total working time of a staff.
For example.
//example 1
$work_start_time = "14:00:00";
$work_end_time = "14:25:00";
//The output should be: 00:25:00
//example 2
$work_start_time = "14:00:00";
$work_end_time = "10:25:00";
//The output should be: 06:25:00
How to achieve the required output. I have no idea.
Detailed answer as requested.
$work_time_start = "8:00:00";
$work_time_end = "18:00:00";
$lunch_break_start = "13:00:00";
$lunch_break_end = "14:00:00";
$worktime = convert_to_seconds($work_time_end) - convert_to_seconds($work_time_start);
$lunchtime = convert_to_seconds($lunch_break_end) - convert_to_seconds($lunch_break_start);
$worktime = $worktime - $lunchtime;
echo $worktime;
function convert_to_seconds($str_time)
{
$str_time = preg_replace("/^([\d]{1,2})\:([\d]{2})$/", "00:$1:$2", $str_time);
sscanf($str_time, "%d:%d:%d", $hours, $minutes, $seconds);
$time_seconds = $hours * 3600 + $minutes * 60 + $seconds;
return $time_seconds;
}
Hope this helps
$work_start_time =new DateTime("14:00:00");
$work_end_time = new DateTime("14:25:00");
$interval = $work_end_time->diff($work_start_time);
echo $interval->format('%H:%I:%S');
Use the following code:
$work_time_start = "8:00:00";
$work_time_end = "18:00:00";
$lunch_break_start = "13:00:00";
$lunch_break_end = "14:00:00";
$total_time = strtotime($lunch_break_start) - strtotime($work_time_start) + strtotime($work_time_end) - strtotime($lunch_break_end);
echo date("h:i:s", mktime(0,0, round($total_time) % (24*3600)));
i have two different break time
default break time
extra break time
here i want to sum of two times and display 12 hrs format
EX :
$default_time = "00:30";
$extra_time = "00:25";
my expected output : 00:55
but now display 01:00
this is my code
$default_time = $work_data->break_time;
$break_time = $work_data->extra_time;
$total_break = strtotime($default_time)+strtotime($break_time);
echo date("h:i",strtotime($total_break));
Here is the function you can calculate total time by passing the arguments to functions.
$hours, $min are supposed variable which is zero
$default_time = "00:30";
$break_time = "00:25";
function calculate_total_time() {
$i = 0;
foreach(func_get_args() as $time) {
sscanf($time, '%d:%d', $hour, $min);
$i += $hour * 60 + $min;
}
if( $h = floor($i / 60) ) {
$i %= 60;
}
return sprintf('%02d:%02d', $h, $i);
}
// use example
echo calculate_total_time($default_time, $break_time); # 00:55
There is one function call to strtotime function too much.
You should leave out the strtotime() call in the last line, as $total_break already is a UNIX timestamp:
$total_break = strtotime($default_time)+strtotime($break_time);
echo date("h:i",$total_break);
The problem is that you're trying to add too specific timestamps, but what you're trying to achieve is adding two durations. So you need to convert those timestamps into durations. For that you need a base, which in your case is 00:00.
$base = strtotime("00:00");
$default_time = $work_data->break_time;
$default_timestamp = strtotime($default_time);
$default_duration = $default_timestamp - $base; // Duration in seconds
$break_time = $work_data->extra_time;
$break_timestamp = strtotime($break_time);
$break_duration = $break_timestamp - $base; // Duration in seconds
$total_break = $default_duration + $break_duration; // 55 min in seconds
// If you want to calculate the timestamp 00:55, just add the base back to it
echo date("H:i", $base + $total_break);
Consider using standard DateTime and DateInterval classes. All you will need is to convert your second variable value to interval_spec format (see http://php.net/manual/en/dateinterval.construct.php for details):
$defaultTime = "00:30";
$breakTime = "PT00H25M"; // Or just 'PT25M'
$totalBreak = (new DateTime($defaultTime))->add($breakTime);
echo $totalBreak->format('H:i');
You could try the following code fragment:
$time1 = explode(":", $default_time);
$time2 = explode(":", $break_time);
$fulltime = ($time1[0] + $time2[0]) * 60 + $time1[1] + $time2[1];
echo (int)($fulltime / 60) . ":" . ($fulltime % 60);
<?php
$time = "00:30";
$time2 = "00:25";
$secs = strtotime($time2)-strtotime("00:00:00");
$result = date("H:i:s",strtotime($time)+$secs);
print_r($result);
?>
Use below code you will definitely get your answers.
$default_time = "00:30:00";
$extra_time = "00:25:00";
$secs = strtotime($extra_time)-strtotime("00:00:00");
$result = date("H:i:s A",strtotime($default_time)+$secs);
echo $result;die;
You can modify above code as per your need.
You could try the following:
$default_time = $work_data->break_time;
$date_start = new DateTime($default_time);
$break_time = $work_data->extra_time;
$interval = new DateInterval("PT" . str_replace(":", "H", $break_time) . "M");
$date_end = $date_start->add($interval);
echo $date_end->format("H:i");
Note that this doesn't account for times which span a 24 hour period
This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 8 years ago.
I have for example:
$first = '2014-05-18 12:00:00';
$last = '2014-05-18 13:30:00';
and i would like calculate interval in 30 minutes for these dates.
For this example interval = 3.
Other example:
$first = '2014-05-18 11:00:00';
$last = '2014-05-18 14:00:00';
interval = 6;
How is the best way for this in PHP?
Simple math solution
$s=floor(abs(strtotime($first)-strtotime($last))/30/60);
Here is fiddle
https://eval.in/153120
function timeDiff30minutes($first,$last){
$first = new Datetime($first);
$last = new Datetime($last);
$time_diff = $first->diff($last);
$time_diff_minute = $time_diff->days * 24 * 60;
$time_diff_minute += $time_diff->h * 60;
$time_diff_minute += $time_diff->i;
return $time_diff_minute/30;
}
timeDiff30minutes('2014-05-18 11:00:00','2014-05-18 14:00:00') // output 6
I can't wrap my brain around this one so I hope someone can help. I have a song track that has the song length in milliseconds. I also have the date the song played in DATETIME format. What I am trying to do is find out how many milliseconds is left in the song play time.
Example
$tracktime = 219238;
$dateplayed = '2011-01-17 11:01:44';
$starttime = strtotime($dateplayed);
I am using the following to determine time left but it does not seem correct.
$curtime = time();
$timeleft = $starttime+round($tracktime/1000)-$curtime;
Any help would be greatly appreciated.
For my needs I used the following approach:
$curTime = microtime(true);
// something time consuming here
...
// get time difference in milliseconds
$timeConsumed = round(microtime(true) - $curTime,3)*1000;
So, the point is that we use float representation of time here (see http://php.net/manual/en/function.microtime.php)
Hope you will adopt it for your needs.
i use the following set of functions for handling mysql dates, maybe they can help you:
function sqlArray($date, $trim=true) {
$result = array();
$result['day'] = ($trim==true) ? ltrim(substr($date,8,2),'0') : substr($date,8,2);
$result['month'] = ($trim==true) ? ltrim(substr($date,5,2),'0') : substr($date,5,2);
$result['year'] = substr($date,0,4);
$result['hour'] = substr($date,11,2);
$result['minutes'] = substr($date,14,2);
return $result;
}
function sqlInt($date) {
$date = sqlArray($date);
return mktime($date['hour'], $date['minutes'], 0, $date['month'], $date['day'], $date['year']);
}
function difference($dateStart, $dateEnd) {
$start = sqlInt($dateStart);
$end = sqlInt($dateEnd);
$difference = $end - $start;
$result = array();
$result['ms'] = $difference;
$result['hours'] = $difference/3600;
$result['minutes'] = $difference/60;
$result['days'] = $difference/86400;
return $result;
}
in your case it should be something like:
$dateplayed = '2011-01-17 11:01:44';
print_r(difference($dateplayed, date('Y:m:d')));
hope it works :D
I have written this function to calculate duration between given two timestamps (with milliseconds).
function calculateTransactionDuration($startDate, $endDate)
{
$startDateFormat = new DateTime($startDate);
$EndDateFormat = new DateTime($endDate);
// the difference through one million to get micro seconds
$uDiff = ($startDateFormat->format('u') - $EndDateFormat->format('u')) / (1000 * 1000);
$diff = $startDateFormat->diff($EndDateFormat);
$s = (int) $diff->format('%s') - $uDiff;
$i = (int) ($diff->format('%i')) * 60; // convert minutes into seconds
$h = (int) ($diff->format('%h')) * 60 * 60; // convert hours into seconds
return sprintf('%.6f', abs($h + $i + $s)); // return total duration in seconds
}
$startDate = '02-Mar-16 07.22.13.000548';
$endDate = '02-Mar-16 07.22.14.000072';
$difference = calculateTransactionDuration($startDate, $endDate);
//Outputs 0.999524 seconds
You could convert the datetime string/input into unixtimestamp and then get the difference. If you do have milliseconds, unixtimestamp would have digits after the decimal. Once you have the difference, you can convert that value back into your date time pattern using function date in php. Below is the link.
Good luck!
http://php.net/manual/en/function.date.php
I used this function for my self:
public function calculateStringTimeToMiliseconds($timeInString)
{
$startTime = new \DateTime("now");
$endDate = new \DateTime($timeInString);
$interval = $startTime->diff($endDate);
$totalMiliseconds = 0;
$totalMiliseconds += $interval->m * 2630000000;
$totalMiliseconds += $interval->d * 86400000;
$totalMiliseconds += $interval->h * 3600000;
$totalMiliseconds += $interval->i * 60000;
$totalMiliseconds += $interval->s * 1000;
return $totalMiliseconds;
}