function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
peterpptpeterppt 

Formatting SF DateTime in php

Is there a simple way to format a date and time currently displayed as 2012-11-06T23:00:00.000Z  as  dd/mm/yyyy hh:mm ?  I've looked at various php formatting functions but can't find one suitable.  I could do this by string manipulation but hope there is a simpler approach !

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
CheyneCheyne

You can use a combination of PHP's strtotime and date functions. 

$oldDate = '2012-11-06T23:00:00.000Z';
$newDate = date('d/m/Y G:i', strtotime($oldDate));

echo $newDate;

 You can find information on PHP's date function (including formatting string parameters) at http://www.php.net/manual/en/function.date.php. Note that you might need to use gmdate instead which returns a GMT datetime. Change the 'G' to a 'g' if you want to display the time in 12 hour format instead of 24 hour format, and you can use an 'a' or 'A' at the end if you want to add the am/pm or AM/PM to the end. 

All Answers

CheyneCheyne

You can use a combination of PHP's strtotime and date functions. 

$oldDate = '2012-11-06T23:00:00.000Z';
$newDate = date('d/m/Y G:i', strtotime($oldDate));

echo $newDate;

 You can find information on PHP's date function (including formatting string parameters) at http://www.php.net/manual/en/function.date.php. Note that you might need to use gmdate instead which returns a GMT datetime. Change the 'G' to a 'g' if you want to display the time in 12 hour format instead of 24 hour format, and you can use an 'a' or 'A' at the end if you want to add the am/pm or AM/PM to the end. 

This was selected as the best answer
peterpptpeterppt

Thanks - I'd done it by some rather crude string manipulation.  This is the elegant solution I'd hope for.

Thanks again.