获取 AWS 实例的运行时间

问题描述:

我正在使用 PHP AWS 开发工具包来获取我账户中所有正在运行的实例.我使用了以下 API:

I am using the PHP AWS SDK for getting all the running instances in my account. I used the following API:

$this->ec2Client = Ec2Client::factory(array(
            'profile' => AWS_PROFILE, //contains my credentials
            'region' => 'ap-northeast-1',
            'version' => 'latest',
        ));

$result = $this->ec2Client->DescribeInstances(array(
            'Filters' => array(
                array('Name' => 'instance-state-name', 'Values' => array('running')),
            )
        ));

我可以通过 LaunchTimeAvailabilityZone 信息获取所有正在运行的实例.

I can get all the running instances with the LaunchTimeand the AvailabilityZone information.

它们的值分别是 2014-10-31T10:58:35+00:00ap-northeast-1a.

The values for them are 2014-10-31T10:58:35+00:00 and ap-northeast-1a respectively.

根据这些信息,我想以分钟为单位计算运行时间.这样做的正确方法是什么?

Based on this information, I want to calculate the running time in minutes. What is the correct way to do this?

我在 v3 中使用以下解决方案:

I solved it using the following in v3:

function interval_in_minutes($start_time){
        return round(abs($start_time->getTimestamp() -
            (new \DateTime)->getTimestamp()) / 60);
    }

$running_time = interval_in_minutes($instance["LaunchTime"]);