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
Related
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
This question already has answers here:
PHP add up two time variables
(7 answers)
Closed 7 years ago.
How to sum up time in php.
For example I have this series of time duration logs:
00:10:00
00:30:10
01:00:50
The total should be 1 hour and 41 minutes
Here is my code:
$log_in = new DateTime($log->log_in);
$log_out = new DateTime($log->log_out);
$diff = $log_out->diff($log_in);
$total += strtotime($diff->format('%H:%i:%s'));
echo $diff->format('%H:%i:%s');
Convert the time into timestamp using strtotime() function. Then manipulate the time according your need and get result in terms of seconds.
Once you get seconds.
For Hour
$hour = $diff % 3600
For Minute
$Minute = ($diff - ( $hour *3600))%60;
i tried most of what is available on stack overflow but none seem to work.
any way i am trying to compare two (date and time formats). and calculate whether their difference is within 5 seconds of each other
the first date is the current date:
$today = date("Y-m-d H:i:s");
the second date is taken from mysql database:
$result = mysql_query("SELECT * FROM BUS_DATA where BusRegID = 'bus'") or die(mysql_error());
$row = mysql_fetch_array($result);
$update_date = $row["update_date"];
the answer should be segmented into years , month , days , hours , minutes ,and seconds portions.
I am running PHP Version 5.3.3
Edit: most answers give result in time frame only , I want to check whether the date matches , then compare if the time of the day is within 5 seconds , than you guys in advance :)
Try this
function getTimes($t1, $t2)
{
$timeFirst = strtotime($t1);
$timeSecond = strtotime($t2);
$differenceInSeconds = $timeSecond - $timeFirst;
$h=0;
$m = floor($differenceInSeconds / 60);
$s = $differenceInSeconds % 60;
if ($m>=60)
{
$h = floor($m / 60);
$m = $m % 60;
}
$tim = $h.':'.$m.':'.$s;
return $tim;
}
it will return difference time in hours:min:sec
use strtotime, it's understands almost any date formats:
$timeDiff = abs(strtotime($today) - strtotime($update_date));
this gives you the difference between dates in seconds.
If you want to know whether it's within 5 seconds then use Timestamps, makes it into a simple int calculation.
$now = time();
$test = strtotime($update_date);
if($now - $test > 5) {
//NOT WITHIN 5 SECONDS
}
This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
PHP calculate days Between 2 different dates [duplicate]
(2 answers)
Closed 9 years ago.
$1date =$row['Date1'];
$2date = $row['Date2'];
$datediff = $1date - $2date;
echo $datediff;
I want to count the days between and put into a table the result(10 dollars for each day passed)
// convert to unix timestamp
$1date = strtotime($row['Date1']);
$2date = strtotime($row['Date2']);
// 86400 seconds in a day
// floor to round down, change to ceil to round up
$datediff = floor(($1date - $2date) / 86400);
$cost = $days * 10;
You could do it in MySQL then work it into a variable. Do the initial call
SELECT TIMESTAMPDIFF('Date1','Date2');
Try:
$days = date_diff(date_create($row['Date1']), date_create($row['Date2']))->format('%a');
$cost = $days * 10;
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Subtracting dates in PHP
I have two Unix timestamps, how can I calculate the total number of days between them?
$timestamp1 = x;
$timestamp2 = y;
$days_elapsed = floor(($timestamp2 - $timestamp1)/86400);
echo $days_elapsed;
Convert them to UNIX-timestamp (if they arent already), then just
$diff = abs($timestamp1 - $timestamp2);
$days = (int) ($diff / 60 / 60 / 24);
something like this should do the trick:
$days = (strtotime($timestamp1)-strtotime($timestamp2))/(60*60*24);
This code should do the trick
$numDays = abs($timeOne - $timeTwo)/60/60/24;