phpdatecloudkitcloudkit-web-services

How can one pass a date to CloudKit via CK Web Services from a PHP script?


I am writing a PHP script to send data to a CloudKit database via CK Web Services. It works well with string data, but I am having trouble passing a date from PHP to a CK Date/Time field.

CK returns dates as a 13-digit TIMESTAMP. So, the following code

$timestamp = $record['created']['timestamp'];
$timestamp = $timestamp/1000;
echo '<td>'.date('m-d-Y H:i:s', $timestamp)."</td>";

echoes out

04-28-2017 12:35:19

Fine and dandy.

So I make the assumption that if CK delivers a 13-digit TIMESTAMP it should accept the same when passed to a Date/Time field.

Alas, passing $dobTimestamp from the following

$dobTimestamp = strtotime($dob) * 1000;

Results in this BAD_REQUEST error

Invalid value, expected type TIMESTAMP

When I go to the CK Dashboard and manually enter $dob, CK returns a value exactly equal to $dobTimestamp so I think passing $dobTimestamp should work . . . but it does not.

I cannot find out what I am supposed to do in Apple's docs. Does anyone know how to pass a date to a CK Date/Time field via Web Services? Hard to imagine there would not be a way to do it.


Solution

  • After a lot of fiddles, here's what works using PHP DateTime:

    $openingDate = $_POST['openingDate']; // is mm/dd/yyyy
    $closingDate = $_POST['closingDate'];
    

    . . .

    // Process Dates
    $openingDateTime = new DateTime($openingDate, new DateTimeZone('America/New_York'));  // Create new DateTime object
    $closingDateTime = new DateTime($closingDate, new DateTimeZone('America/New_York'));
    
    date_time_set($openingDateTime, 10, 00); // Set time for date object
    date_time_set($closingDateTime, 17, 00); // Set time for date object
    
    $oDate = (date_timestamp_get($openingDateTime)*1000);  // Get the TIMESTAMP from DateTiem object and convert to milliseconds
    $cDate  = (date_timestamp_get($closingDateTime)*1000);
    

    The JSON for the CK Query is simple:

    . . .

    "openingDate": {"value":'.$oDate.'}, 
    "closingDate": {"value":'.$cDate.'},