Francesco Francesco - 3 months ago 23
PHP Question

Store date values in Google Datastore

I'm trying to store a date value in Google Datastore using the Google API PHP Client but the service replies always with an error:

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

Here's the interesting section of the code I use:


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;
}

Answer

According to the Cloud Datastore docs, the dateTimeValue property must be a string that is RFC 3339 formatted):

dateTimeValue: string (RFC 3339 formatted, with milliseconds, for instance 2013-05-14T00:01:00.234Z)