I have custom installation profile and I must set date formats programmatically.
I tried:
db_insert('date_formats')
->fields(array(
'format' => $format_long, // PHP Date String
'type' => 'custom', // 'custom'
'locked' => 0, // 0 = user-specified date format
))
->execute();
variable_set('date_format_long', $format_long);
and
$format_long = 'l, Y-m-d G:h';
system_date_format_save(array(
'is_new' => 1,
'type' => 'custom',
'format' => $format_long,
'locked' => 0,
));
and set this up:
variable_set('date_format_long', $format_long);
variable_set('date_format_medium', $format_medium);
variable_set('date_format_short', $format_short);
At admin/config/regional/date-time all seems to be fine but:
time is e.g 16:04 (now I have 16:38). Time changing, but on settings page time is still the same (after refresh).
I tried to create a content and at admin/content listing I have content updated date as 16:04 too... :(
If I switch to default formats all works ok.
What is wrong?
EDIT
At admin/config/regional/settings time is 16:04 too....
EDIT
I created module based on suggestion from answer, still no luck :(
define('RPKE_DATE_FORMAT_LONG', 'l, Y-m-d G:h');
define('RPKE_DATE_FORMAT_MEDIUM', 'D., Y-m-d G:h');
define('RPKE_DATE_FORMAT_SHORT', 'Y-m-d G:h');
define('RPKE_DATE_DATE_TYPE_LONG', 'rpke_date_long');
define('RPKE_DATE_DATE_TYPE_MEDIUM', 'rpke_date_medium');
define('RPKE_DATE_DATE_TYPE_SHORT', 'rpke_date_short');
/**
* Implements hook_date_format_types().
*/
function rpke_date_date_format_types() {
$types = array();
$types[RPKE_DATE_DATE_TYPE_LONG] = t('Long date format for RPKE');
$types[RPKE_DATE_DATE_TYPE_MEDIUM] = t('Medium date format for RPKE');
$types[RPKE_DATE_DATE_TYPE_SHORT] = t('Short date format for RPKE');
return $types;
}
function rpke_date_date_formats() {
return array(
array(
'type' => RPKE_DATE_DATE_TYPE_LONG,
'format' => RPKE_DATE_FORMAT_LONG,
'locales' => array(),
),
array(
'type' => RPKE_DATE_DATE_TYPE_MEDIUM,
'format' => RPKE_DATE_FORMAT_MEDIUM,
'locales' => array(),
),
array(
'type' => RPKE_DATE_DATE_TYPE_SHORT,
'format' => RPKE_DATE_FORMAT_SHORT,
'locales' => array(),
),
);
}
function rpke_date_flush_caches() {
_rpke_date_purge_date_formats();
}
function _rpke_date_purge_date_formats() {
$format = system_get_date_formats(RPKE_DATE_DATE_TYPE_LONG);
variable_set('date_format_' . RPKE_DATE_DATE_TYPE_LONG, RPKE_DATE_FORMAT_LONG);
if (!empty($format['dfid'])) {
system_date_format_delete($format['dfid']);
}
$format = system_get_date_formats(RPKE_DATE_DATE_TYPE_MEDIUM);
variable_set('date_format_' . RPKE_DATE_DATE_TYPE_MEDIUM, RPKE_DATE_FORMAT_MEDIUM);
if (!empty($format['dfid'])) {
system_date_format_delete($format['dfid']);
}
$format = system_get_date_formats(RPKE_DATE_DATE_TYPE_SHORT);
variable_set('date_format_' . RPKE_DATE_DATE_TYPE_SHORT, RPKE_DATE_FORMAT_SHORT);
if (!empty($format['dfid'])) {
system_date_format_delete($format['dfid']);
}
}
EDIT
The worst nightmare is that creating custom date format from UI (e.g. custom date format in Views) result display of wrong date too....