如何获得以毫秒为单位的时差 [英] How to get time difference in milliseconds

查看:84
本文介绍了如何获得以毫秒为单位的时差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法绕开这个大脑,所以我希望有人能帮上忙.我有一首歌曲,歌曲的长度以毫秒为单位.我还以DATETIME格式播放了歌曲的日期.我想做的是找出歌曲播放时间还剩下几毫秒.

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.

示例

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

任何帮助将不胜感激.

推荐答案

我使用以下一组函数来处理mysql日期,也许它们可以为您提供帮助:

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')));

希望它能起作用:D

这篇关于如何获得以毫秒为单位的时差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆