Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Format time to ISO 8601 time with fractional seconds in PHP

Posted on 2008-10-27
Last Modified: 2008-10-29
I've tried using the date('c', $theTime) and it returns an ISO 8601 formatted time, but removes the fractional seconds from the time.

Here's what I'm using:
    $theTime = '2008-08-18 17:59:15.27172';
    $date = date('c', strtotime($theTime));
    echo ($date);

It simply prints:

The time format is correct, accept I want to keep in the fractional seconds.  It does not seem to be the call to date() that does it, it's the strtotime call that removes the fractional seconds from what I can tell.  Is there an alternative to this?

"It can't be done" is an acceptable aswer if you can show me some documentation somewhere where it states/implies that.
Question by:AngryLoop
  • 4
  • 3
  • 2

Expert Comment

ID: 22815745
Off the top of my head I can't say why that wouldn't work, as I don't think it's the strtotime() call that is the issue. As you can, the date() format by default (using the "c" param) does not actually have a micro time before the -06:00 bit.

Instead, you would need to construct your own date / time and use the "u" param in addition to the other params to give yourself your desired time format.

For example, this should work (it will take the current timestamp as I have not given it a second param):

echo date("o\-m\-d\TH\:i\:s\.uP");

However, it gives the following output:


As you can see it's not actually giving you the micro seconds for some reason, which I am not entirely sure about. The PHP documentation on date() does say that the "u" param should work if you're PHP version is 5.2.2 or later, which mine is.

So the simple answer to your question is, it should work, but unfortunately somewhere in the date() function or (possibly, though unlikely) in the strtotime() function that microsecond value is getting lost.

Sorry that I couldn't be of more help :(
echo date("o\-m\-d\TH\:i\:s\.uP");

Open in new window

LVL 109

Expert Comment

by:Ray Paseur
ID: 22816099
ISO8601 time does not have microseconds, but this code snippet might be useful.  Note that time() does not return fractional seconds.

Man Pages here:

HTH, ~Ray
$now = microtime();
$date = date("Y-m-d\tH:i:s\.u", $now);
echo $date;

Open in new window


Author Comment

ID: 22816664
ISO8601 does have microseconds..  It has several different formats, but one of them includes "fractional" seconds.  See http://www.w3.org/TR/NOTE-datetime

That aside, microtime() doesn't help me in this instance, because I'm getting a time from a database so I can't use the current time.  The date/time is formatted like: 2008-08-19 08:52:03.12371

The reason I said strtotime() is that if you call just that (meaning not the date function) and echo it, it removes the fractional seconds.

Your solution works by adding in ".000000" after the seconds.  It removes any fractional seconds.  Is there a conversion method which does NOT remove fractional seconds?
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.


Accepted Solution

DarkFish earned 250 total points
ID: 22816777
The point I was making was that even if you could get strtotime() to return a timestamp with fractional microseconds included, the date() function doesn't want to work with it anyway.

I don't know of any native PHP function that can handle microseconds correctly and convert it to a valid timestamp and then format that into a date. Neither do I know of an external PHP library that can handle date and time formatting with microseconds included, though I'm sure there is one somewhere.

However, that said you can write your own code to format your date using RegEx; for instance, over at the PHP documentation page for date() ( http://uk2.php.net/manual/en/function.date.php ) one member has given this some thought (please see code snippet.)

The original can be found at in the comments section by "Cortexd" on "27-Aug-2008 06:47".

Hope that helps,
27-Aug-2008 06:47
a date function supporting the milliseconds format character
function udate($format, $utimestamp = null)
    if (is_null($utimestamp))
        $utimestamp = microtime(true);
    $timestamp = floor($utimestamp);
    $milliseconds = round(($utimestamp - $timestamp) * 1000000);
    return date(preg_replace('`(?<!\\\\)u`', $milliseconds, $format), $timestamp);
echo udate('H:i:s.u'); // 19:40:56.78128
echo udate('H:i:s.u', 654532123.04546); // 16:28:43.45460

Open in new window

LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 22816839
Sorry - I should have said PHP's implementation of ISO8601, as shown in the date() functions that cover ISO 8601 date (added in PHP 5) - c, r, and u - do not have microseconds.  As I recall, the ISO standard has tenths and hundredths of seconds, but not microseconds.

However if you are getting the value from a data base in that format, you can explode the date/time with the delimiter '.' then take the seconds via strtotime on explode[0] and the milliseconds on explode[1]

Then if you wanted to get it back to the way it looked coming out of the data base, you could use something like the code snippet.

If you are doing math with these values you will probably need to keep the seconds and microseconds in separate computations.

$now = {string from DB as shown in original post};
$explode = explode('.', $now);
$isodate = explode[0];
$usecond = explode[1];
$date = date("Y-m-d\TH:i:s", strtotime($isodate));
$date .= ".$usecond";
echo $date;

Open in new window


Author Comment

ID: 22817071
So it sounds like it's not directly supported in PHP.  The solution would be to use the built in functions to get close, and then I'd have to add in the fractional time information myself via regex or something similar.
LVL 109

Expert Comment

by:Ray Paseur
ID: 22817376
Not sure I understand what you are trying to do. Give us a little more information about the app.  The examples posted above teach how to carry, extract and recombine the fields.  What are we missing?

Author Comment

ID: 22817573
I'm not really sure what more relevant information I can give.

What I'm trying to do is take the time I've got and convert it to ISO 8601 without losing fractional seconds.

My hope by posting it here was to find a different way to pass the information to the built in functions date() or strtotime(), or a different function altogether.

From your posts, what I'm finding is that PHP does not seem to support it directly in that I can't use date('c', $unixTime); or any other built in function and maintain the fractional seconds (ISO 8601 date (added in PHP 5) - c, r, and u - do not have microseconds).

If I use the method you provided, it drops the time zone information - understandably so, as it would require a little more parsing effort to get the microseconds in the right place while including the timezone info (using date('c', $unixTime)).

That is why I was drawing the conclusion that it's a matter of manipulating the date/time given by the built in function date() since date() time does not include microseconds.

I just wanted to most elegant approach, but it sounds like the best I can do is add in a hack to add the fractional seconds back in to the date/time after it's been converted to ISO 8601.
LVL 109

Expert Comment

by:Ray Paseur
ID: 22820794
You might want to give this information to the folks at PHP as a suggestion - you can also write your own PHP functions (easy way: use PHP; sophisticated way: use C++).  I think if you extend DarkFish's code example to add the date portion, you've got a foundation.

If you are looking for ways to do arithmetic with the ISO-Plus-Microtime values, it would be easy enough to integrate a few basic math calls, but that would be a different question.

In any case, good luck with your project. ~Ray

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question