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 Yii locale which is based on CLDR as far as i know.
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 should one fix such issue?
This is the fix:
$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');
The idea is to use the PHP's DateTime::format()
method to extract the timestamp that has taken into consideration the user timezone. Then, based on this timestamp, format according to Yii datetime formatting.
Well, nothing strange, your date format is M/d/yy h:mm a
, and according to DateTime::format()
documentation :
M
: A short textual representation of a month, three lettersy
: A two digit representation of a yearYii does not use the same format : http://www.unicode.org/reports/tr35/tr35-dates.html#Date_Format_Patterns
You should simply use CDateFormatter::format()
: http://www.yiiframework.com/doc/api/1.1/CDateFormatter#format-detail