tags:

views:

91

answers:

2

PHP : How to get milisecond between two dateTime obj? $date = new DateTime(); $date2 = new DateTime("1990-08-07 08:44");

I try to follow the comment below but I got an error $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();

I get an error "Call to undefined method DateTime::getTimestamp()"

+2  A: 

DateTime dates are only stored as whole seconds. If you still need the number of milliseconds between two DateTime dates, then you can use getTimestamp() to get each time in seconds (then get the difference and turn it into milliseconds):

$seconds_diff = $date2.getTimestamp() - $date.getTimestamp()
$milliseconds_diff = $seconds_diff * 1000
BudgieInWA
-1 Lol.. yes, seconds times 1000 is milliseconds.
Fosco
I probably could have mentioned what Saul mentioned :)
BudgieInWA
+1 since this really is the only method (in concept at least) to take two second-precision dates and get the number of milliseconds between them...
ircmaxell
@Saul hehe.. 1 second is 1000 milliseconds.. it's not going to get you any more precision than seconds, but it is a valid answer.
Fosco
@Fosco: Yup, I was thinking about precision but looking at your 1st comment at the same time, hence the mixup.
Saul
Thanks you guys ^^
Prince OfThief
sorry this get an error$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();"Call to undefined method DateTime::getTimestamp()"
Prince OfThief
+9  A: 

In the strict sense, you can't.

It's because the smallest unit of time for the DateTime class is a second.

If you need a measurement containing milliseconds then use microtime()


Edit:

On the other hand if you simply want to get the interval in milliseconds between two ISO-8601 datetimes then one possible solution would be

function millisecsBetween($dateOne, $dateTwo, $abs = true) {
    $func = $abs ? 'abs' : 'intval';
    return $func(strtotime($dateOne) - strtotime($dateTwo)) * 1000;
}

Beware that by default the above function returns absolute difference. If you want to know whether the first date is earlier or not then set the third argument to 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);

On systems where the size of time datatype is 32 bits, such as Windows7 or earlier, millisecsBetween is only good for dates between 1970-01-01 00:00:00 and 2038-01-19 03:14:07 (see Year 2038 problem).

Saul
From the manual microtime() returns the current date time.
Preet Sangha
So does $date = new DateTime();
Saul
Are you sure DateTime uses a UNIX timestamp internally? I thought it could represent a wider range of dates.
JW
@JW: Although I was thinking more along the lines of a format as having a specific smallest unit, you make a valid point. The actual values for DateTime can certainly go below 1970 and referring to the UNIX timestamp might suggest otherwise. Fixed it.
Saul