3

i'm wondering if there's a way to get any datetime in microseconds.

I was looking forward microtime(), but it just returns the date in the moment.

Anyone knows if is this possible?

I have my date given like: Y-m-d H:i:s.u.

I was thinking about something like (Y-1970)*31556926 + m*151200+d*86400+h*3600+m*60+s.u

but I don't know if that's why i'm a beginner on programming, but i can't think in a way to separate each: Y,m... to do the math.

Would appreciate any help/suggestions.

GustavoxD
  • 89
  • 8

3 Answers3

5

You can do this with DateTime:

$date = DateTime::createFromFormat('Y-m-d H:i:s.u', '2000-01-01 13:12:12.129817');
echo $date->format('U.u');
//prints: 946728732.129817
Pitchinnate
  • 7,307
  • 1
  • 18
  • 35
  • DateTime does not support fractional seconds, so I believe the fractional component would be lost with this approach. You could use this to get the whole second value though. – Mike Brant Oct 07 '13 at 21:29
  • I thought that was the case but I just tested it and it worked correctly. – Pitchinnate Oct 07 '13 at 21:30
  • 1
    Really? That is interesting. The documentation commentary would seem to suggest otherwise. Maybe this is now supported. If that is the case, then yet another reason why DateTime and DateInterval should be THE date/time tools of choice for PHP developers. – Mike Brant Oct 07 '13 at 21:31
  • If you get the chance please test also I am running php5.5 I would be interested to see if it isn't supported in earlier versions. – Pitchinnate Oct 07 '13 at 21:33
  • must be a really new feature : http://3v4l.org/TC0Ck ;) it is not working, it gives u always `0000` – hek2mgl Oct 07 '13 at 21:34
  • I tested multiple versions of PHP on here http://sandbox.onlinephpfunctions.com/code/5cc3406840b0065eeb7f268cd5958b41e2ea92fc seemed to work correctly in each. – Pitchinnate Oct 07 '13 at 21:34
  • Yeah, but you don't output the current time. Use `microtime()` as I suggested. – hek2mgl Oct 07 '13 at 21:36
  • OP doesn't want current time, he already has it, he just wants to change formatting. At least that is what I understand from OP. "I have my date given like: Y-m-d H:i:s.u." – Pitchinnate Oct 07 '13 at 21:37
  • Seems to work in 5.3.1 and above http://3v4l.org/P3n6v if you aren't running that new of a version I recommend going with Mike Brant's answer. – Pitchinnate Oct 07 '13 at 21:41
  • The question was hard to get, especially after the comments below my answer. I thought he needs the current time in usec.. however, now all are happy... – hek2mgl Oct 07 '13 at 21:48
1

What I would do is simply split apart your input format like this:

Y-m-d H:i:s and u

You should be able to do this by exploding on . in your input formatted date string. Then just calculate the UNIX timestamp on the whole second portion. Finally just add your fraction second portion back to the timestamp (via either string concatenation of arithmetic depending on whether you want string or float as result).

Mike Brant
  • 66,858
  • 9
  • 86
  • 97
0

No it's not possible. Timestamps are integers internally, describing the seconds from 01.01.1970 GMT. Regardless if you are using a 32 or 64 bit system, the microseconds from 1970 would lead to an overflow as there has much too many of them gone.


You updated the question.. Yes, it is possible to display the current time in microsends using microtime():

$microtime = microtime();
// will return something like:
// 0.40823900 1381181037
// where the first are the micros and the last a regular time stamp
list($micros, $timestamp) = explode(' ', $microtime);

echo date('Y-m-d H:i:s', intval($timestamp)) . '+' 
  . (floatval($micros) * 1000000) . 'msec';
hek2mgl
  • 133,888
  • 21
  • 210
  • 235