Search code examples
phprestbasecamp

How to convert 7 digit timestamp in php?


I am working on an API and I get the below response. It's basecamp 3 API.

[
   'access_token' => 'BAhbB0kiAbB7ImNsaWVudF9pZCI6ImUxZTk1NTYxMzdlZDIwYTQzZGEyYjk4NGU1NWEyYjM4Y2ExNWRlODQi'
   'expires_in' => 1209600
]

I don't know to which kind of date format expires_in belongs. I want to convert this timestamp to a standard DateTime string like below.

Required Format

1209600 => 2018-06-23 14:09:01


Solution

  • What gets passed in expires_in is the interval of token validity in seconds. 1209600 in your example is equal to 14 days (1209600 / (3600 * 24)).

    Here's one possible approach to convert this into real date:

    $value = 1209600;
    $date = new DateTime();
    $date->add(DateInterval::createFromDateString( $value . ' seconds') );
    echo $date->format('Y-m-d H:i:s');