Unable to set column of type Date using Parse.com PHP SDK - invalid type

时间:2015-10-06 09:01:49

标签: php parse-platform sdk

I'm pretty new to Parse.com. I've been using the PHP SDK to try and populate some data. I've had no issues so far other than not being able to set date values against objects that have a column of type "Date".

My code:

$uploadDate = date("Y-m-d\TH:i:s.u",$instaPost->created_time);
echo $uploadDate;
$photo->set('uploadDate',$uploadDate);
$photo->save();

The echo call returns: 2015-10-06T19:33:36.000000

Parse gives me an error of: Failed to create new object, with error message: invalid type for key uploadDate, expected date, but got string

I have also tried:

$uploadDate = array(
    "__type" => "Date",
    "iso" => date("c", time())
);

and various other combinations and date formats (the first parameter of the date() function).

Does anyone know the correct way of doing this??

2 个答案:

答案 0 :(得分:2)

Php date()函数返回字符串。所以$ uploadDate的类型为string。但是当您尝试将其设置为列类型日期时,它会给出错误。这应该可以解决你的问题。

$date = array(
  "__type" => "Date",
  "iso" => date("c", $instaPost->created_time)
 ); 
$photo->set('uploadDate',$date);
$photo->save();

答案 1 :(得分:2)

我遇到了同样的问题。你正确地做到了,但你错过了一件事。

而不是使用: $光电>设置( 'uploadDate',$ uploadDate);

试试这个: $光电> setArray( 'uploadDate',$ uploadDate);

我也在使用我在parse.com论坛上找到的这个功能:

function getProperDateFormat($value)
  {
    $dateFormatString = 'Y-m-d\TH:i:s.u';
    $date = date_format($value, $dateFormatString);
    $date = substr($date, 0, -3) . 'Z';
    return $date;
  }

其中$ value是一个php Date对象。

所以这就是我把所有这些放在一起的方式:

$date = new DateTime($year."-".$month."-".$day);
$puzzle->setArray( "date", ["__type" => "Date", "iso" => getProperDateFormat($date)] );

它对我来说很完美