2011-01-17 44 views
6

Tôi không thể quấn bộ não của mình xung quanh cái này vì vậy tôi hy vọng ai đó có thể giúp đỡ. Tôi có một bài hát có độ dài bài hát tính bằng mili giây. Tôi cũng có ngày bài hát được phát ở định dạng DATETIME. Những gì tôi đang cố gắng làm là tìm hiểu xem còn bao nhiêu phần nghìn giây trong thời gian chơi bài hát.Cách nhận chênh lệch thời gian bằng mili giây

Ví dụ

$tracktime = 219238; 
$dateplayed = '2011-01-17 11:01:44'; 
$starttime = strtotime($dateplayed); 

Tôi đang sử dụng sau đây để xác định thời gian còn lại nhưng nó dường như không đúng.

$curtime = time(); 
$timeleft = $starttime+round($tracktime/1000)-$curtime; 

Mọi trợ giúp sẽ được đánh giá cao.

+6

http://php.net/manual/en/function.microtime.php – thetaiko

+1

Ý bạn là gì "có vẻ không đúng"? –

+0

xem xét DATETIME chỉ có độ chính xác xuống thứ hai và không phải là mili giây ... điều này sẽ khá khó khăn. – dqhendricks

Trả lời

1

tôi sử dụng các thiết lập sau đây của các chức năng để xử lý ngày mysql, có lẽ họ có thể giúp bạn:

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

trong trường hợp bạn cần một cái gì đó như:

$dateplayed = '2011-01-17 11:01:44'; 
print_r(difference($dateplayed, date('Y:m:d'))); 

hy vọng nó hoạt động: D

+0

Không tệ, chỉ cần một vài bình luận khác sẽ tốt hơn ... Khác với mã hóa tốt đó! – ItsMeDom

0

Bạn có thể chuyển đổi chuỗi/chuỗi ngày giờ thành unixtimestamp và sau đó nhận được sự khác biệt. Nếu bạn có mili giây, thì unixtimestamp sẽ có các chữ số sau dấu thập phân. Khi bạn có sự khác biệt, bạn có thể chuyển đổi giá trị đó trở lại mẫu thời gian ngày của bạn bằng cách sử dụng ngày chức năng trong php. Dưới đây là liên kết.

Chúc may mắn!

http://php.net/manual/en/function.date.php

14

Đối với nhu cầu của tôi tôi đã sử dụng phương pháp sau đây:

$curTime = microtime(true); 
// something time consuming here 
... 
// get time difference in milliseconds 
$timeConsumed = round(microtime(true) - $curTime,3)*1000; 

Vì vậy, vấn đề là chúng ta sử dụng đại diện phao thời gian ở đây (xem http://php.net/manual/en/function.microtime.php)

Hy vọng bạn sẽ áp dụng nó cho nhu cầu của bạn.

0

tôi đã sử dụng chức năng này để tự tôi:

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

Nó luôn trả về 0. – JCarlos

0

Tôi đã viết hàm này để tính thời gian giữa cho hai timestamps (với mili giây).

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 
Các vấn đề liên quan