Search code examples
amazon-ec2aws-php-sdk

Getting running time for an AWS instance


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

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

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?


Solution

  • 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"]);