9

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.

jason45
  • 123
  • 2
  • 3
  • 5
  • 6
    http://php.net/manual/en/function.microtime.php – thetaiko Jan 17 '11 at 16:11
  • 1
    What do you mean by "does not seem correct"? – Ciprian L. Jan 17 '11 at 16:17
  • considering DATETIME only has precision down to the second, and not millisecond... this would be pretty tough. – dqhendricks Jan 17 '11 at 16:29
  • Thanks for the reply. I am using the $timeleft variable in a javascript setTimeout but it does not correctly run the function after the time left. The time left is too fast, it updates in about 2 seconds instead of the correct amount of time. – jason45 Jan 17 '11 at 16:32
  • Another solution I could use is to get the time left in seconds then convert the seconds to milliseconds. – jason45 Jan 17 '11 at 16:38

5 Answers5

30

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.

de_niska
  • 301
  • 3
  • 3
1

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
Naresh Chennuri
  • 1,133
  • 11
  • 10
1

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

Raul Leaño Martinet
  • 2,035
  • 6
  • 28
  • 44
0

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;
}
entymon
  • 62
  • 5
0

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

macha
  • 7,337
  • 19
  • 62
  • 84