Date and time
Date and time are stored in model fields of "date" and "date_time" types. In the database, (no matter the type of database) they are kept in a regular SQL format of "yyyy-mm-dd hh:mm:ss". During inserting and extracting data from a database you need to use special methods to adjust values to the necessary format, by using class "I18n" which is also responsible for internationalization and localization of a content.
The main format of date is specified in the file of localization which is located in "/adminpanel/i18n/your_locale/locale.php" file.
- formatDate($date [, $format]) - transforms the date from SQL format (2012-09-15 12:54:23) to the current format, specified in localization file, if you pass an optional parameter in the form of "only-date", it will return only date, and if you pass "no-seconds", seconds will be removed. Also the date format can be passed in the format similar to PHP "date()" function, for example "d-m-Y"
- dateForSQL($date) - transforms the date from current format into SQL format (2012-09-15 12:54:23), to insert into a database
- getCurrentDate([$sql_format]) - returns the current date according to a localization format. If you pass an optional parameter in the form of "SQL", the current date will be returned in regular SQL format
- getCurrentDateTime([$sql_format]) - returns the current date and time according to a localization format
- timestampToDate($timestamp) - transforms a timestamp into date according to a localization format
- getMonth($number) - returns the name of the month by it's number, for example "February", "March"
- getMonthCase($number) ) - returns the name of the month in a genitive case, for languages other than English
Samples
//Find news record by id $record = $mv -> news -> findRecordById(32); //Display date, transforming from SQL format echo I18n :: formatDate($record -> date); //Special format of date echo I18n :: formatDate($record -> date, "d/m/Y")); //Display date and time without seconds echo I18n :: formatDate($record -> date, "no-seconds"); //Display only date echo I18n :: formatDate($record -> date, "only-date"); //Pass the current date into news record $record -> date = I18n :: getCurrentDateTime(); $record -> update(); //Pass the current date into news record via model object (saving changes history) $news -> read(47) -> setValue("date", I18n :: getCurrentDateTime()) -> update(); //Count the number of news for current date $number = $news -> countRecords(array("date" => I18n :: getCurrentDate("SQL"))); //Transform date from SQL into "12 November 2011" public function formatDate($date) { $date = explode("-", $date); return $date[2]." ".I18n :: getMonthCase(intval($date[1]))." ".$date[0]; }
Previous section
Files and images