Php 如何获得两个日期时间对象之间的毫秒?

Php 如何获得两个日期时间对象之间的毫秒?,php,datetime,Php,Datetime,如何在两个DateTime对象之间获得毫秒 $date = new DateTime(); $date2 = new DateTime("1990-08-07 08:44"); 我试图按照下面的评论,但我得到了一个错误 $stime = new DateTime($startTime->format("d-m-Y H:i:s")); $etime = new DateTime($endTime->format("d-m-Y H:i:s")); $millisec = $etime

如何在两个
DateTime
对象之间获得毫秒

$date = new DateTime();
$date2 = new DateTime("1990-08-07 08:44");
我试图按照下面的评论,但我得到了一个错误

$stime = new DateTime($startTime->format("d-m-Y H:i:s"));
$etime = new DateTime($endTime->format("d-m-Y H:i:s")); 
$millisec = $etime->getTimestamp() - $stime->getTimestamp();` 
我得到了错误

调用未定义的方法DateTime::getTimestamp()


日期时间日期仅存储为整秒。如果您仍然需要两个DateTime日期之间的毫秒数,则可以使用getTimestamp()以秒为单位获取每个时间(然后获取差值并将其转换为毫秒):


严格来说,你不能

这是因为DateTime类的最小时间单位是秒

如果需要包含毫秒的测量值,请使用


编辑:

另一方面,如果您只是想得到两个时间间隔的毫秒数,那么一个可能的解决方案是

function millisecsBetween($dateOne, $dateTwo, $abs = true) {
    $func = $abs ? 'abs' : 'intval';
    return $func(strtotime($dateOne) - strtotime($dateTwo)) * 1000;
}
请注意,默认情况下,上述函数返回绝对差值。如果您想知道第一个日期是否更早,那么将第三个参数设置为false

// Outputs 60000
echo millisecsBetween("2010-10-26 20:30", "2010-10-26 20:31");

// Outputs -60000 indicating that the first argument is an earlier date
echo millisecsBetween("2010-10-26 20:30", "2010-10-26 20:31", false);

在时间数据类型大小为32位的系统上,如Windows7或更早版本,毫秒间隔仅适用于
1970-01-01 00:00:00
2038-01-19 03:14:07
(请参阅)。

很抱歉,我提出了一个老问题,但我找到了一种从DateTime对象中获取毫秒时间戳的方法:

function dateTimeToMilliseconds(\DateTime $dateTime)
{
    $secs = $dateTime->getTimestamp(); // Gets the seconds
    $millisecs = $secs*1000; // Converted to milliseconds
    $millisecs += $dateTime->format("u")/1000; // Microseconds converted to seconds
    return $millisecs;
}
但是,它要求DateTime对象包含微秒(格式为u):

这只在PHP5.2中起作用,因此添加了对DateTime的微秒支持

使用此函数,您的代码将如下所示:

$date_str = "1990-08-07 20:46:00.588";
$date1 = DateTime::createFromFormat("Y-m-d H:i:s.u", $date_str);

$msNow = (int)microtime(true)*1000;

echo $msNow - dateTimeToMilliseconds($date1);

DateTime自5.2.2以来支持微秒。date函数的文档中提到了这一点,但这里需要重复。您可以创建带有小数秒的日期时间,并使用“u”格式字符串检索该值

<?php
// Instantiate a DateTime with microseconds.
$d = new DateTime('2011-01-01T15:03:01.012345Z');

// Output the microseconds.
echo $d->format('u'); // 012345

// Output the date with microseconds.
echo $d->format('Y-m-d\TH:i:s.u'); // 2011-01-01T15:03:01.012345

// Unix Format
echo "<br>d2: ". $d->format('U.u');

function get_data_unix_ms($data){
    $d = new DateTime($data);
    $new_data = $d->format('U.u');
    return $new_data;
}

function get_date_diff_ms($date1, $date2)
    {
        $d1 = new DateTime($date1);
        $new_d1 = $d1->format('U.u');

        $d2 = new DateTime($date2);
        $new_d2 = $d2->format('U.u');

        $diff = abs($new_d1 - $new_d2);

        return $diff;
    }

这里有一个函数可以完成这项功能+测试


我可能已经提到了索尔提到的:)+1,因为这确实是唯一一种方法(至少在概念上)获取两秒精度的日期,并获得它们之间的毫秒数…@Saul-hehe。。1秒等于1000毫秒。。这不会比秒更精确,但这是一个有效的答案。@Fosco:是的,我在考虑精度,但同时查看了您的第一条评论,因此出现了混淆。很抱歉,这会出现错误$stime=new DateTime($startTime->format(“d-m-Y H:I:s”)$etime=newdatetime($endTime->format(“d-m-Y H:i:s”)$毫秒=$etime->getTimestamp()-$stime->getTimestamp();“调用未定义的方法DateTime::getTimestamp()”$date=new DateTime()也是如此;您确定DateTime在内部使用UNIX时间戳吗?我认为它可以代表更广泛的日期范围。@JW:虽然我认为格式应该有一个特定的最小单位,但你的观点是正确的。DateTime的实际值肯定会低于1970,而引用UNIX时间戳可能表明情况并非如此。修正了它。
DateTime
目前支持亚秒精度-例如:
echo DateTime::createFromFormat(“U.U”,“1234567890.987654”)->格式(“Y-m-d H:i:s.U”)
输出
2009-02-13 23:31:30.987654
。带有URL的简短答案被认为是低质量的,因为外部链接可能会改变或中断。请查看并编辑您的答案,以包含链接中的关键详细信息。
$date_str = "1990-08-07 20:46:00.588";
$date1 = DateTime::createFromFormat("Y-m-d H:i:s.u", $date_str);

$msNow = (int)microtime(true)*1000;

echo $msNow - dateTimeToMilliseconds($date1);
<?php
// Instantiate a DateTime with microseconds.
$d = new DateTime('2011-01-01T15:03:01.012345Z');

// Output the microseconds.
echo $d->format('u'); // 012345

// Output the date with microseconds.
echo $d->format('Y-m-d\TH:i:s.u'); // 2011-01-01T15:03:01.012345

// Unix Format
echo "<br>d2: ". $d->format('U.u');

function get_data_unix_ms($data){
    $d = new DateTime($data);
    $new_data = $d->format('U.u');
    return $new_data;
}

function get_date_diff_ms($date1, $date2)
    {
        $d1 = new DateTime($date1);
        $new_d1 = $d1->format('U.u');

        $d2 = new DateTime($date2);
        $new_d2 = $d2->format('U.u');

        $diff = abs($new_d1 - $new_d2);

        return $diff;
    }