We want to show the date / time in the timezone of the user, but also in the right locale. Unfortunately, strftotime doesn’t seem available for the DateTime object. So I ended up with something like this… I assume you did setlocale somewhere else in your project to make sure PHP is in the right locale.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
/** * @param $date Date, best in Y-m-d H:i:s format * @param $format Format according to strftime * @param null $timezone Optional timezone to convert to * @return string */ function formatDate($date, $format, $timezone=null) { $dt = new \DateTime($date); // Convert to other timezone if ($timezone !== null) $dt->setTimeZone(new \DateTimeZone($timezone)); return strftime($format, strtotime($dt->format('Y-m-d H:i:s'))); } |