Format Date according to User’s Date Format.

sugarcrm-logo
SugarCrm

Quite recently, I faced an issue wherein I have to push some calculated date(based on some algo) to the date field on EditView of a custom module.

My custom function calculated date in dd/mm/yyyy format but the user can have any possible date formats defined by SugarCRM.

Before pushing my calculated date i need to format this date according to user’s settings.

I came through this intelligent post from Johndope http://johndopenotes.wordpress.com/2013/03/05/sugarcrm-format-date-according-to-user-timezone/.

I did little modifications in the code and hurray it solved my problem.

global $current_user; // Instantiate the TimeDate Class
$timeDate = new TimeDate();
$sampleDate = '2014-08-28'; // Sample date.
// Call the function
$FormatedDate = $timeDate->to_display_date($sampleDate, true, true, $current_user);
echo "Date->".$FormatedDate;
//If you want to get time too use the following code.
$sampleDateTime = '2014-08-28 08:00:00'; // Sample date.
// Call the function
$FormatedDateTime = $timeDate->to_display_date_time($sampleDateTime, true, true, $current_user);
echo "DateTime->".$FormatedDateTime;

 

 

Advertisements