Crear un módulo MyCustom
myCustom.module contiene
/**
* Implements hook_date_formats().
*/
function myCustom_date_formats() {
$formats = array('g:i a', 'H:i', 'M j', 'j M', 'm/d/y', 'd/m/y', 'j/n/y', 'n/j/y');
$types = array_keys(myCustom_date_format_types());
$date_formats = array();
foreach ($types as $type) {
foreach ($formats as $format) {
$date_formats[] = array(
'type' => $type,
'format' => $format,
'locales' => array(),
);
}
}
return $date_formats;
}
/**
* Implements hook_date_format_types().
*/
function myCustom_date_format_types() {
return array(
'myCustom_current_day' => t('MyCustom: Current day'),
'myCustom_current_year' => t('MyCustom: Current year'),
'myCustom_years' => t('MyCustom: Other years'),
);
}
/**
* Formats a timestamp according to the defines rules.
*
* Examples/Rules:
*
* Current hour: 25 min ago
* Current day (but not within the hour): 10:30 am
* Current year (but not on the same day): Nov 25
* Prior years (not the current year): 11/25/08
*
* @param $timestamp
* UNIX Timestamp.
*
* @return
* The formatted date.
*/
function myCustom_format_date($timestamp) {
if ($timestamp > ((int)(REQUEST_TIME / 3600)) * 3600) {
return t('@interval ago', array('@interval' => format_interval(abs(REQUEST_TIME - $timestamp), 1)));
}
if ($timestamp > ((int)(REQUEST_TIME / 86400)) * 86400) {
return format_date($timestamp, 'myCustom_current_day');
}
if ($timestamp > mktime(0, 0, 0, 1, 0, date('Y'))) {
return format_date($timestamp, 'myCustom_current_year');
}
return format_date($timestamp, 'myCustom_years');
}
Crea myCustom.install y contiene
/**
* @file
* Install file for myCustom.module
*/
/**
* Implements hook_install().
*/
function myCustom_install() {
// Define default formats for date format types.
variable_set("date_format_myCustom_current_day", 'g:i a');
variable_set("date_format_myCustom_current_year", 'M j');
variable_set("date_format_myCustom_years", 'n/j/y');
}
/**
* Implements hook_uninstall().
*/
function myCustom_uninstall() {
variable_del('date_format_myCustom_current_day');
variable_del('date_format_myCustom_current_year');
variable_del('date_format_myCustom_years');
}
Uso:
echo myCustom_format_date(1392532844);