Google API TimeStamp protocol

1.1k Views Asked by At

I'm trying to retrieve metrics from Google My Business API.

However I cant figure out what to pass as a timestamp for the time periods.

The error I'm getting is this...

Invalid value at 'basic_request.time_range.end_time' (type.googleapis.com/google.protobuf.Timestamp),
  Field 'endTime', Invalid data type for timestamp, value is 1606780800

My code is this

$time = new \Google_Service_MyBusiness_TimeRange;
$start = strtotime("2020-01-01");
$end = strtotime("2020-12-01");

$time->setStartTime($start);
$time->setEndTime($end);

In the class for the TimeRange it shows they simply must be timestamps

class Google_Service_MyBusiness_TimeRange extends \Google_Model
{
  protected $internal_gapi_mappings = array( );
  /* @params Unix Timestamps */
  private $endTime;
  private $startTime;

However... I was looking at this. https://developers.google.com/protocol-buffers/docs/reference/google.protobuf#google.protobuf.Timestamp

And it shows that you have to pass something on the lines of Timestamp(seconds, nanos). So it seems like google wants an array of the seconds, and the nano seconds??

Heres other docs to help https://developers.google.com/my-business/reference/rpc/google.mybusiness.v4#google.mybusiness.v4.TimeRange

Anyone run into this problem?

2

There are 2 best solutions below

0
On

The documentation you link to says:

Range is from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z.

Suggesting that you should use a similar format.

$time = new \Google_Service_MyBusiness_TimeRange;

$start = \DateTime::createFromFormat(
        "y-m-d H:i:s",
        "2020-01-01 00:00:00",
        new \DateTimeZone("UTC")
    )
    ->format("Y-m-d\TH:i:s\Z");

$end = \DateTime::createFromFormat(
        "y-m-d H:i:s",
        "2020-11-30 23:59:59",
        new \DateTimeZone("UTC")
    )
    ->format("Y-m-d\TH:i:s\Z");

$time->setStartTime($start);
$time->setEndTime($end);

I wasn't able to find much about this library online, but what little there is does follow this format. "Z" at the end indicates UTC, and I've hard-coded it in my example. It should be able to get replaced with your local timezone, but you may have to try perhaps "O" or "P" in the format string.

0
On

Here is some working code:

Note, $startDate and $endDate are just strings passed in from a jQuery calendar, ex "12/27/2020".

The key here is to use DATE_ATOM, which produces a timestamp in the format 2020-12-27T13:22:12+00:00

$gmbStartDate = date(DATE_ATOM, strtotime($startDate . " 12:01 AM "));
$gmbEndDate = date(DATE_ATOM, strtotime($endDate . " 11:59 PM "));
$time = new Google_Service_MyBusiness_TimeRange();
$time->setStartTime($gmbStartDate);
$time->setEndTime($gmbEndDate);
$basicMetricsRequest->setTimeRange($time);

Note that the timestamps are expressed as UTC, so in this example, the insights may be slightly off based on the time zone for the business. If you want to get the insights for your time zone, you'll have to add/subtract the appropriate number of seconds for your timezone.