2

When you describe your EC2 instances, you get an XML node of:-

[launchTime] => 2011-10-14T09:22:37.000Z

I'd like to use this command with PHP, to measure the number of seconds the instance has been on and take actions.

It seems to me there's a number of ways to break this down, including explodes and string searches and regex. But, what is the best way?

waxical
  • 3,826
  • 8
  • 45
  • 69

2 Answers2

4
$ts = DateTime::CreateFromFormat('Y-m-d\TH:i:s?????', '2011-10-14T09:22:37.000Z');
echo $ts->diff(new DateTime())->format('U');

assuming you're on PHP 5.3+

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • Thanks Marc, looks nice and simple. I have 5.3.8, yet this says "PHP Fatal error: Class 'Date' not found"? – waxical Nov 16 '11 at 17:53
  • Thanks again, Marc. Unfortunately, I still get nothing out of this. I'd love to offer some more insight to that, but basically all I see is a non-object for diff, so that's the first sign of it not working. Have you got it working? – waxical Nov 16 '11 at 18:18
  • Hmm. appears that the `.000Z` at the end of the string is making the DateTime parser b0rk, so it's returning false. – Marc B Nov 16 '11 at 18:37
  • or `?????` works too, except that's five digits and microseconds can vary from 2-6, I think ;) – Kato Nov 16 '11 at 18:39
  • shame that U doesn't work; can use it with all the other DateTime classes and formats :( – Kato Nov 16 '11 at 19:12
3

FINAL ANSWER:

Okay, after checking out this thread, I've decided on this approach as the only one that seems to return an accurate measure:

$dt = DateTime::createFromFormat('Y-m-d\TH:i:s.u\Z', $date);
$now = new DateTime();
echo ($now->getTimestamp() - $dt->getTimestamp())."\n";

ATTEMPTS:

In PHP 5.3 using classes (no 'U' format):

$dt = DateTime::createFromFormat('Y-m-d\TH:i:s.u\Z', $arr['launchTime']);
echo (new DateTime())->format('U');

In PHP 5.3 using procedural calls (also works like final solution):

$dt = date_create_from_format('Y-m-d\TH:i:s.u\Z', $arr['launchTime']);
$now = date_create();
echo ($now->getTimestamp() - $dt->getTimestamp());

In any version using strtotime (return wrong time):

date_default_timezone_set('UTC');
echo time() - strtotime($arr['launchTime']);
Community
  • 1
  • 1
Kato
  • 40,352
  • 6
  • 119
  • 149
  • 1
    hm, I just noticed that 'U' doesn't seem to work with DateInterval (works with all the other date formats); annoying... back to the drawing tablet... – Kato Nov 16 '11 at 18:48
  • Yeah, I'm not sure strtotime is returning an accurate measure; it doesn't seem to do leap years and daylight savings accurately, [according to this thread](http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php) – Kato Nov 16 '11 at 19:09
  • You're right, it's currently BST (+1 hours from GMT) and using the above means I have to manually add 1 hour to it currently. Apart from that, and also that I'm pretty sure constructor dereferencing is a PHP 5.4 feature (not 5.3), all good! – Jimbo Oct 11 '13 at 10:56