zend-frameworktimezonezend-locale

Time conversion using Zend locale


Is there a way of converting a time, say 17:00 to 5:00pm using Zend Locale?

I've tried the method in the docs as it is (which has a typo), but it doesn't work. It gives the error 'Unable to parse date '13:44:42' using 'dd.MM.yyyy' (M <> y)'

$locale = new Zend_Locale('de_AT');
if (Zend_Locale_Format::getTime('13:44:42',
                            array('date_format' =>
                                      Zend_Locale_Format::STANDARD,
                                  'locale' => $locale))) {
    print "time";
} else {
    print "not a time";
}

I then tried a 2 step method, getting the time format of the current locale first, and then using that in the getTime function.

$locale = new Zend_Locale('en_US');
$tf = Zend_Locale_Format::getTimeFormat($locale);
$test = Zend_Locale_Format::getTime('17:00', array('date_format' => $tf, 'locale' => $locale));

This returns a result but just gives me back what I had

array('date_format'=>'h:mm:ss a', 'locale'=>'en_US', 'hour'=>'17', 'minute'=>'00')

Is there something that will convert the time to the actual locale I'm trying to parse it to?


Solution

  • You need to be using Zend_Date with the locale to get the date in the format you want.

    $date    = new Zend_Date(); // Default ISO format type & en_US
    // Set the time and pass the format I am using to set the time
    $date->setTime('17:00:00', 'HH:mm:ss'); 
    echo $date; // Jul 15, 2011 5:00:00 PM
    

    EDIT

    More on how you can use Zend_Locale with Zend_Date

    $locale = new Zend_Locale('de_AT');
    echo  $date->toString(Zend_Locale_Format::getTimeFormat($locale)) ; // 17:00:00
    $locale = new Zend_Locale('en_US');
    echo  $date->toString(Zend_Locale_Format::getTimeFormat($locale)) ; // 5:00:00 PM