【问题标题】:Store date values in Google Datastore在 Google Datastore 中存储日期值
【发布时间】:2014-08-18 12:48:25
【问题描述】:

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

无效值:无效格式:“2014-08-18 12:40:52”在“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;
}

【问题讨论】:

    标签: php google-app-engine google-cloud-datastore


    【解决方案1】:

    根据Cloud Datastore docsdateTimeValue 属性必须是 RFC 3339 格式的字符串):

    dateTimeValue:字符串(RFC 3339 格式,以毫秒为单位,例如2013-05-14T00:01:00.234Z

    【讨论】:

    • 谢谢帕特里克。我错过了文档。我使用日期(“Y-m-d\TH:i:sP”);它奏效了。
    【解决方案2】:

    你在哪里:

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

    替换为:

    $time = date(DATE_RFC3339);
    

    这将为您提供格式化为符合 RFC 3339 的日期时间,这是 Google Cloud Datastore dateTimeValue 的格式

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2020-10-03
        • 1970-01-01
        • 2016-04-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-07
        • 2013-02-22
        • 2015-11-27
        相关资源
        最近更新 更多