Home » Php » php – How to get time difference in milliseconds

php – How to get time difference in milliseconds

Posted by: admin July 12, 2020 Leave a comment

Questions:

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.

How to&Answers:

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 😀

Answer:

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.

Answer:

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

Answer:

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;
}

Answer:

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