Properly Format Date/time

I have this code




$dateTime = new DateTime('@'.strtotime('+30 minutes'));

$dateTime->setTimezone(new DateTimeZone($someModel->timezone));

$otherModel->send_at = $dateTime->format($otherModel->getDateTimeFormat());



Where $otherModel->getDateTimeFormat() returns "M/d/yy h:mm a" which is fine because it is based on the current locale.

Now, when i pass this format to PHP’s datetime::format class method [$dateTime->format($otherModel->getDateTimeFormat())] i get this result: “Dec/06/1313 03:1212 pm” which is looking weird because the format that php accepts for date/datetime is not the same as the one Yii is using in it’s locales.

How can i fix this ?

Here is the fix for those who might hit the issue:




$dateTime = new DateTime('@'.strtotime('+30 minutes'));

$dateTime->setTimezone(new DateTimeZone($someModel->timezone));


// get a timestamp from the current date that also knows about the offset.

$timestamp = CDateTimeParser::parse($dateTime->format('Y-m-d H:i:s'), 'yyyy-MM-dd HH:mm:ss');


// now format using Yii's methods and format type

$otherModel->send_at = Yii::app()->dateFormatter->formatDateTime($timestamp, 'short', 'short');