PHP解析Amazon EC2实例“launchTime” [英] PHP Parsing Amazon EC2 instance 'launchTime'

查看:164
本文介绍了PHP解析Amazon EC2实例“launchTime”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当你描述你的EC2实例,你会得到一个XML节点: -

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

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

我想使用这个命令用PHP,衡量秒的情况下已经对数,并采取行动。

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?

推荐答案

最后的答案:

好,退房后这个线程,我已经决定这种方法,因为这似乎返回一个准确的衡量只有一个:

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";

尝试:

在PHP 5.3使用类(无'U'格式):

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');

在PHP 5.3使用过程调用(也就像最终的解决方案):

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

在任何版本的使用的strtotime (返回错误的时间):

In any version using strtotime (return wrong time):

date_default_timezone_set('UTC');
echo time() - strtotime($arr['launchTime']);

这篇关于PHP解析Amazon EC2实例“launchTime”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆