AnsweredAssumed Answered

Date Time format inconsistent!! Current User date format is not applied if the request is from API. e.g.  date_entered

Question asked by Marc Yu on Oct 27, 2015
I am using SugarCRM 6.5 professional.

Date Time format inconsistent!! Current User date format is not applied if the request is from API. e.g.  the retrieved value of date_entered of a product in an API method VS  in any  logic hook method

To be more specific:

User XX selected the date format 'd-m-Y' in the profile setting page.

In any code when the request is from UI, if i use the following code:
============================================================
global $current_user;       
$dateFormat = $current_user->getPreference('datef');
error_log(dateFormat);

$TEMP_PRODUCT = new Product();             
$TEMP_PRODUCT->retrieve('149744bc-c121-5624-3ddc-562db523c2f7');
error_log($TEMP_PRODUCT->date_entered);
=============================================================
THE OUTPUT IS: 
d-m-Y
26-10-2015 04:10pm

However, if the request comes from API method call, (same user name and password was used to login from API and have got the session id), THE OUTPUT IS:
d-m-Y
2015-10-26 05:10:41
In this case, it seems even though the current user is existed. the profile is ignored??? I had tried to insert "$current_user->loadPreferences();" before i retrieve the object, but it still does not help.

Could anyone explain this??? 
I have a need to do a date time check in the code, so i need to convert the date time string to object first: 
global $current_user;        
$dateFormat = $current_user->getPreference('datef');
$tempDateObj = DateTime::createFromFormat($dateFormat, $TEMP_PRODUCT->date_entered);

IT WORKS FINE if the request is from UI for different users. But is FAILS if the request is from API calls.

Currently i am thinking to use if(!empty($_SERVER['HTTP_REFERER'])) to check if it is from UI, but i am not sure i can trust it.

Outcomes