Probably, when we are making our custom modules in Magento, we need to work with date and/or time fields in our admin forms. We recommend using Zend_Date for this purpose because their flexibility. In this post, you can find different functions based on the use of Zend_Date which have been of great help for us!
/** * functions_zend_data.php : transformation of time & datetime fields. */ /** * function diffSeconds: returns seconds of difference between two date time fields. * * $dateTime1 datetime * $dateTime2 datetime * * @return integer */ public function diffSeconds($dateTime1,$dateTime2) { //*** Datetime in Zend Format $localdatetime1 = new Zend_Date(); $localdatetime2 = new Zend_Date(); $localdatetime1->set($dateTime1, Zend_Date::DATETIME); $localdatetime2->set($dateTime2, Zend_Date::DATETIME); //*** retrieve difference in SECONDS format. $interval = $localdatetime1->sub($localdatetime2); $measure = new Zend_Measure_Time($interval->toValue(), Zend_Measure_Time::SECOND); return $measure->getValue(); } /** * function secondsToTime: transform a value in seconds to a time value (HH:mm:ss). * * $totSeconds int * @return string */ public function secondsToTime($totSeconds) { //>>> Calculate values of: hours,minutes, seconds. $seconds = $totSeconds % 60; $totMinutes = ($totSeconds - $seconds) / 60; $minutes = $totMinutes % 60; $totHours = ($totMinutes - $minutes) / 60; $hours = $totHours % 24; return ($hours > 0 ? $hours . ":" : "00:").($minutes > 0 ? $minutes . ":" : "00:") . $seconds; } /** * function timeToString: Transform a time value in order to show it on a time field ($fieldset->addField) in an admin form of Magento. * * $timefield time * @return string */ public function timeToString($timefield) { return (string) substr($timefield,0,2).','.substr($timefield,3,2).','.substr($timefield,6,2); } /** * function getCurrentDateTime: Returns current datetime value. * * @return string */ public function getCurrentDateTime() { $localDate = new Zend_Date(); return (string) $localDate->toString(Zend_Date::DATETIME); } /** * function getCurrentTime: Returns current time value. * * @return string */ public function getCurrentTime() { $date = new Zend_Date(); return (string) $date->toString(Zend_Date::TIMES); } /** * function getWeekDay: Returns current day name. * * @return string */ public function getWeekDay() { $date = new Zend_Date(); $day = $date->toString(Zend_Date::WEEKDAY_8601); switch ($day) { case '0': return (string)'Sunday'; break; case '1': return (string)'Monday'; break; case '2': return (string)'Tuesday'; break; case '3': return (string)'Wednesday'; break; case '4': return (string)'Thursday'; break; case '5': return (string)'Friday'; break; case '6': return (string)'Saturday'; break; default: return (string)'Nothing'; break; } }