Amazon bills you for the full hour of EC2 usage even if you have only run the instance for 10 minutes. So before terminating any unused EC2 instances I want to check if the launch time
- current time
is under 10 minutes.
Let me explain this to you by an example:
Let's say I launch my EC2 instance at: 2016-Feb-01 13:02:22
. Then I would like to terminate it only when the time is 10 minutes from the launch time (regardless of the date). So if it is 16:58:54
then it means that current time is a good time to terminate it because I will be billed at 17:02:22
again (in about 4 minutes as per this example)? On the other hand if the time is 13:03:01
then it is not a good time since I'm already billed for the hour.
Here is what I could come up with, but it's not working when the current time is 58
and launch time is 02
, etc
<?php
$launch = '2016-03-03 13:02:22';
$now = '2016-03-04 12:58:22';
$epochLaunch = strtotime(sprintf('1970-01-01 00:%02d:00', date('i', strtotime($launch))));
$epochNow = strtotime(sprintf('1970-01-01 00:%02d:00', date('i', strtotime($now))));
echo $epochLaunch - $epochNow;