在Google Datastore中存储日期值


Store date values in Google Datastore

我正在尝试使用Google API PHP客户端在Google数据存储中存储日期值,但服务总是回复错误:

Invalid value for: Invalid format: "2014-08-18 12:40:52" is malformed at " 12:40:52 .

下面是我使用的代码中有趣的部分:
function create_entity($name, $property, $data) {
    $entity = new Google_Service_Datastore_Entity();
    $entity->setKey(createKey($name));
    $string_prop = new Google_Service_Datastore_Property();
    $string_prop->setStringValue($data);
    $string_prop->setIndexed(false);
    $time = date("Y-m-d H:i:s");
    $string_date = new Google_Service_Datastore_Property();
    $string_date->setDateTimeValue($time);
    $string_date->setIndexed(false);
    $property_map = [];
    $property_map[$property] = $string_prop;
    $property_map['date'] = $string_date;
    $entity->setProperties($property_map);
    return $entity;
}
function create_commit($name, $property, $data) {
    $entity = create_entity($name, $property, $data);
    $mutation = new Google_Service_Datastore_Mutation();
    $mutation->setUpsert([$entity]);
    $req = new Google_Service_Datastore_CommitRequest();
    $req->setMode('NON_TRANSACTIONAL');
    $req->setMutation($mutation);
    return $req;
}

根据Cloud Datastore文档,dateTimeValue属性必须是RFC 3339格式的字符串:

dateTimeValue:字符串(RFC 3339格式化,毫秒,例如2013-05-14T00:01:00.234Z)

哪里有:

$time = date("Y-m-d H:i:s");

替换为:

$time = date(DATE_RFC3339);

这将为您提供符合RFC 3339格式的日期时间,这是谷歌云数据存储dateTimeValue的格式

是否可以在本地时间存储日期时间?我尝试用时区偏移的iso 8601格式存储。它将值记录为字符串类型。当您在数据存储UI中创建实体时,您可以将datetime存储为本地时间。你可以在本地时间使用数据存储节点.js模块存储日期时间吗?