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

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?
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.


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() ( ) 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

815 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now