我正在尝试从Google My Business API中检索指标。
然而,我想不出要传递什么作为时间段的时间戳。
我得到的错误是...
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我的代码是
$time = new \Google_Service_MyBusiness_TimeRange;
$start = strtotime("2020-01-01");
$end = strtotime("2020-12-01");
$time->setStartTime($start);
$time->setEndTime($end);在TimeRange的类中,它显示它们必须是时间戳
class Google_Service_MyBusiness_TimeRange extends \Google_Model
{
protected $internal_gapi_mappings = array( );
/* @params Unix Timestamps */
private $endTime;
private $startTime;它表明你必须在时间戳行上传递一些东西(秒,nanos)。因此,谷歌似乎想要一组秒,而纳秒??
有人遇到过这个问题吗?
发布于 2020-11-28 07:53:11
您链接到的文档显示:
范围为0001-01-01T00:00:00Z到9999-12-31T23:59:59.999999999Z。
建议您使用类似的格式。
$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);我在网上找不到很多关于这个库的信息,但what little there is确实遵循这种格式。末尾的"Z“表示UTC,我已经在我的示例中对其进行了硬编码。它应该能够替换为您的本地时区,但您可能必须尝试在格式字符串中使用"O“或"P”。
发布于 2020-12-28 00:08:15
以下是一些工作代码:
注意,$startDate和$endDate只是从jQuery日历传入的字符串,例如"12/27/2020“。
这里的关键是使用DATE_ATOM,它会生成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);请注意,时间戳以UTC表示,因此在此示例中,根据业务的时区,洞察力可能会略有偏差。如果你想了解你的时区,你必须根据你的时区加/减适当的秒数。
https://stackoverflow.com/questions/65042188
复制相似问题