2

I'm retrieving the "LastModifiedDate" attribute of a File through a GET request with the Salesforce REST API but I just can't figure out the date format.

Salesforce's documentation states the date format is ISO8601 but when I do the following in PHP :

// The value I retrieve is "2014-11-20T14:23:44.000+0000"
$date = \Datetime::createFromFormat(\Datetime::ISO8601, [value]);

PHP throws an error saying it couldn't create a date with this format and this value.

Do you know what is the actual date format used by Salesforce in its REST API ?

Thanks for your answers.

Cheers.

  • Docs say The date and time should be provided in ISO 8601 format: YYYY-MM-DDThh:mm:ss+hh:mm. Try changing your time to 2014-11-20T14:23:44.00+00:00 and see if it works – Novarg Nov 24 '14 at 14:10
  • Hey Novarg, thanks for the answer :) Unfortunately the time is coming directly from Salesforce and that's only once I got it that I'm trying to create a date object with PHP. So even if I change it manually for testing purposes and PHP manages to create a date object, that still doesn't fix my problem since I won't be able to change manually all the date values I receive from Salesforce everytime I ask for a date value with the REST API. Any other idea though ? – Asticode Nov 24 '14 at 14:35
  • Could this be something you are looking for? search.cpan.org/~carlvince/DateTime-Format-Salesforce-0.01_01/… – Novarg Nov 24 '14 at 14:41
  • Hey Novarg, I think what you've found is the best I can get since there are not a lot of documentation about this strange behavior. At least it confirms that Salesforce actually sends the wrong date format. I'll try to come up with a solution and post it here. Thanks again :) – Asticode Nov 24 '14 at 15:06
3

According to the documentation here http://php.net/manual/en/datetime.createfromformat.php you can try this

<?php
    $value = "2014-11-20T14:23:44.000+0000"; // example value
    $value = substr($value,0,19);
    $date = new DateTime();
    $date = DateTime::createFromFormat('Y-m-d?H:i:s', $value);
    print( "TEST<br> ".$value ."<br>" ); 
    print_r( $date );    
?>

To respect the timezone, you need a bit more string cutting. It looks like the milliseconds needs to be removed an the offset need a ":" inbetween. Look for the string functions here http://php.net/manual/en/book.strings.php

  • Unfortunately that would make me lose the information about the timezone since you're removing the last "+0000" of the value. Check out the answer I posted and let me know if you feel that's OK :) – Asticode Nov 24 '14 at 15:17
  • Edited my answer. Would not use regexp here, even if it works. Consider readability and maintainability. – RichardT Nov 24 '14 at 15:20
1

Here's the "dirty" solution I've implemented : upon receiving the date, I preg replace the pattern /\.[\d]{3}/ (removing the milliseconds) and then I'm good to go to create a date from the format ISO8601.

So instead of :

// The value I retrieve is "2014-11-20T14:23:44.000+0000"
$date = \Datetime::createFromFormat(\Datetime::ISO8601, [value]);

I do :

// The value I retrieve is "2014-11-20T14:23:44.000+0000"
$date = \Datetime::createFromFormat(\Datetime::ISO8601, preg_replace('/\.[\d]{3}/', '', [value]);

Thanks to Novarg and RichartT for pointing me to the right direction.

I won't be able to accept my own answer right away so if you feel there's a better way to solve this, please don't hesitate and if your solution is better than mine, I'll accept it as the correct one :)

  • I believe that you're missing a closed parenthesis in your second code snippet. – clone45 Aug 30 '17 at 21:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.