/    Sign up×
Community /Pin to ProfileBookmark

Change am/pm to a.m./p.m. in Drupal/ProsePoint?

First, a caveat: My knowledge of PHP is minimal at best at the moment; I have bought a couple of books, which I am starting to read through, but I feel like I’m still in the “learning my ABCs” stage … definitely not fluent in PHP yet.

I am designing a test site in ProsePoint (Drupal), and I would like for times to display as a.m. and p.m. instead of the PHP default of am and pm.

I did some research online about this a few weeks ago, and I stumbled across some script or bit of freeware that someone had written for just such a purpose … but now I can’t find it again.

And even if I did … I’m not quite sure where in the ProsePoint/Drupal installation I might need to put it in order for it to work.

Any ideas or suggestions?

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@JerailJan 16.2010 — You could do something like this:
[code=php]$meridian = (date('a') == 'am') ? 'a.m.' : 'p.m.';[/code]
As far as I know, that would be simplest way to do it, and then you could display your time like this:
[code=php]echo date('g:i ') . $meridian;[/code]

I think this should work. Perhaps somebody else would know a more efficient way of doing it.
Copy linkTweet thisAlerts:
@News_designauthorJan 17.2010 — Thanks. That looks like it may take care of half the solution: the "how." My next task is to figure out the other half: the "where." Because I'm not fluent in PHP, it may take me a while to figure out where in all the PHP files that make up the site that information needs to go in order to work correctly.

Maybe I can find someone on one of the Drupal forums that can help me with that.

Thanks again for your help!
Copy linkTweet thisAlerts:
@News_designauthorJan 18.2010 — Okay, the module in Drupal responsible for handling dates is Date API. I don't know enough PHP to really know what I'm looking at (or looking for), but I did discover this list of "cases" in the date_api_elements.inc file:

[CODE]$final_date = array('hour' => 0, 'minute' => 0, 'second' => 0,
'month' => 0, 'day' => 0, 'year' => 0);
foreach ($letters as $i => $letter) {
$value = $values[$i];
switch ($letter) {
case 'd':
case 'j':
$final_date['day'] = intval($value);
break;
case 'n':
case 'm':
$final_date['month'] = intval($value);
break;
case 'F':
$array_month_long = array_flip(date_month_names());
$final_date['month'] = $array_month_long[$value];
break;
case 'M':
$array_month = array_flip(date_month_names_abbr());
$final_date['month'] = $array_month[$value];
break;
case 'Y':
case 'y':
$year = $value;
// if no century, we add the current one ("06" => "2006")
$final_date['year'] = str_pad($year, 4, substr(date("Y"), 0, 2), STR_PAD_LEFT);
break;
case 'a':
case 'A':
$ampm = strtolower($value);
break;
case 'g':
case 'h':
case 'G':
case 'H':
$final_date['hour'] = intval($value);
break;
case 'i':
$final_date['minute'] = intval($value);
break;
case 's':
$final_date['second'] = intval($value);
break;
case 'U':
return date_convert($value, DATE_UNIX, DATE_DATETIME);
break;
}
}
if (isset($ampm) && $ampm == 'pm' && $final_date['hour'] < 12) {
$final_date['hour'] += 12;
}
elseif (isset($ampm) && $ampm == 'am' && $final_date['hour'] == 12) {
$final_date['hour'] -= 12;
}[/CODE]


I'm wondering if it might be possible to adjust something in here to set up the "a.m./p.m." display. Am I looking in the right place? Or do I need to keep looking?
Copy linkTweet thisAlerts:
@JerailJan 18.2010 — I don't know anything about Drupal, but as long as you back up your files, there's no harm in playing around with the code a little. Try doing this, and see what happens:[code=php] if (isset($ampm) && $ampm == 'pm' && $final_date['hour'] < 12) {
$final_date['hour'] += 12;
$ampm = 'p.m.';
}
elseif (isset($ampm) && $ampm == 'am' && $final_date['hour'] == 12) {
$final_date['hour'] -= 12;
$ampm = 'a.m.';
}[/code]
Copy linkTweet thisAlerts:
@News_designauthorJan 18.2010 — There are a number of lines related to am/pm in the module itself. Not being fluent in PHP, I'm not really sure what they all do, so I've copied them here. If you need more of the code (there's a LOT of code in the entire module), let me know.

[CODE]
/**
* An array of am and pm options.
* @param $required
* If not required, returned array will include a blank value.
* @return array an array of am pm options.
*/
function date_ampm($required = FALSE) {
$none = array('' => '');
$ampm = array('am' => date_t('am', 'ampm'), 'pm' => date_t('pm', 'ampm'));
return !$required ? $none + $ampm : $ampm;
}
[/CODE]


[CODE]
/**
* A function to translate ambiguous short date strings.
*
* Example: Pass in 'Monday', 'day_abbr' and get the translated
* abbreviation for Monday.
*
* @param string $string
* @param string $context
* @param int $langcode
* @return translated value of the string
*/
function date_t($string, $context, $langcode = NULL) {
//static $replace = array();

if (empty($replace[$langcode])) {
// The function to create the date string arrays is kept separate
// so those arrays can be directly accessed by other functions.
date_t_strings($replace, $langcode);
}
switch ($context) {
case 'day_name':
case 'day_abbr':
case 'day_abbr1':
case 'day_abbr2':

$untranslated = array_flip(date_week_days_untranslated());
break;
case 'month_name':
case 'month_abbr':
$untranslated = array_flip(date_month_names_untranslated());
break;

case 'ampm':
$untranslated = array_flip(array('am', 'pm', 'AM', 'PM'));

break;
case 'datetime':
$untranslated = array_flip(array('Year', 'Month', 'Day', 'Week', 'Hour', 'Minute', 'Second', 'All Day', 'All day'));

break;
case 'datetime_plural':
$untranslated = array_flip(array('Years', 'Months', 'Days', 'Weeks', 'Hours', 'Minutes', 'Seconds'));

break;
case 'date_order':
$untranslated = array_flip(array('Every', 'First', 'Second', 'Third', 'Fourth', 'Fifth'));

break;
case 'date_order_reverse':
$untranslated = array_flip(array('', 'Last', 'Next to last', 'Third from last', 'Fourth from last', 'Fifth from last'));

break;
case 'date_nav':
$untranslated = array_flip(array('Prev', 'Next', 'Today'));

break;
}
$pos = $untranslated[$string];
return $replace[$langcode][$context][$pos];
}
[/CODE]


[CODE]
/**
* Construct translation arrays from pipe-delimited strings.
*
* Combining these strings into a single t() gives them the context needed
* for better translation.
*/
function date_t_strings(&$replace, $langcode) {
$replace[$langcode]['day_name'] = explode('|', trim(t('!day-name Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday', array('!day-name' => ''), $langcode)));
$replace[$langcode]['day_abbr'] = explode('|', trim(t('!day-abbreviation Sun|Mon|Tue|Wed|Thu|Fri|Sat', array('!day-abbreviation' => ''), $langcode)));
$replace[$langcode]['day_abbr1'] = explode('|', trim(t('!day-abbreviation S|M|T|W|T|F|S', array('!day-abbreviation' => ''), $langcode)));
$replace[$langcode]['day_abbr2'] = explode('|', trim(t('!day-abbreviation SU|MO|TU|WE|TH|FR|SA', array('!day-abbreviation' => ''), $langcode)));
$replace[$langcode]['ampm'] = explode('|', trim(t('!ampm-abbreviation am|pm|AM|PM', array('!ampm-abbreviation' => ''), $langcode)));
$replace[$langcode]['datetime'] = explode('|', trim(t('!datetime Year|Month|Day|Week|Hour|Minute|Second|All Day|All day', array('!datetime' => ''), $langcode)));
$replace[$langcode]['datetime_plural'] = explode('|', trim(t('!datetime_plural Years|Months|Days|Weeks|Hours|Minutes|Seconds', array('!datetime_plural' => ''), $langcode)));
$replace[$langcode]['date_order'] = explode('|', trim(t('!date_order Every|First|Second|Third|Fourth|Fifth', array('!date_order' => ''), $langcode)));
$replace[$langcode]['date_order_reverse'] = explode('|', trim(t('!date_order |Last|Next to last|Third from last|Fourth from last|Fifth from last', array('!date_order' => ''), $langcode)));
$replace[$langcode]['date_nav'] = explode('|', trim(t('!date_nav Prev|Next|Today', array('!date_nav' => ''), $langcode)));
[/CODE]


[CODE]
$max = strlen($format);
$datestring = '';
for ($i = 0; $i < $max; $i++) {
$c = $format[$i];
switch ($c) {
// Use date_t() for ambiguous short strings that need translation.
// We send long day and month names to date_t(), along with context.
case 'l':
$datestring .= date_t(date_format($date, 'l'), 'day_name', $langcode);
break;
case 'D':
$datestring .= date_t(date_format($date, 'l'), 'day_abbr', $langcode);
break;
case 'F':
$datestring .= date_t(date_format($date, 'F'), 'month_name', $langcode);
break;

case 'M':
$datestring .= date_t(date_format($date, 'F'), 'month_abbr', $langcode);
break;

case 'A':
case 'a':
$datestring .= date_t(date_format($date, $c), 'ampm', $langcode);
break;

// The timezone name translations can use t().

[/CODE]


[CODE]
function date_convert($date, $from_type, $to_type, $tz = 'UTC') {
if (empty($date) && !$date === 0) return NULL;
if (empty($from_type) || empty($to_type) || $from_type == $to_type) return $date;
switch ($from_type) {
case DATE_ARRAY:
if (!is_array($date)) return NULL;
if (isset($date['ampm'])) {
if ($date['ampm'] == 'pm' && $date['hour'] < 12) $date['hour'] += 12;
if ($date['ampm'] == 'am' && $date['hour'] == 12) $date['hour'] -= 12;
}
[/CODE]


[CODE]
/**
* Implementation of hook_elements().
*/
function date_api_elements() {
require_once('./'. drupal_get_path('module', 'date_api') .'/date_api_elements.inc');
return _date_api_elements();
}

function date_api_theme() {
$path = drupal_get_path('module', 'date_api');
$base = array(
'file' => 'theme.inc',
'path' => "$path/theme",
);
return array(
'date_nav_title' => $base + array('arguments' => array('type' => NULL, 'view' => NULL)),
'date_vcalendar' => $base + array('arguments' => array('events' => NULL, 'calname' => NULL)),
'date_vevent' => $base + array('arguments' => array('event' => NULL)),
'date_valarm' => $base + array('arguments' => array('alarm' => NULL)),
'date_timezone' => $base + array('arguments' => array('element' => NULL)),
'date_select' => $base + array('arguments' => array('element' => NULL)),
'date_text' => $base + array('arguments' => array('element' => NULL)),
'date_select_element' => $base + array('arguments' => array('element' => NULL)),
'date_textfield_element' => $base + array('arguments' => array('element' => NULL)),
'date_date_part_hour_prefix' => $base + array('arguments' => array('element' => NULL)),
'date_part_minsec_prefix' => $base + array('arguments' => array('element' => NULL)),
'date_part_label_year' => $base + array('arguments' => array('element' => NULL)),
'date_part_label_month' => $base + array('arguments' => array('element' => NULL)),
'date_part_label_day' => $base + array('arguments' => array('element' => NULL)),
'date_part_label_hour' => $base + array('arguments' => array('element' => NULL)),
'date_part_label_minute' => $base + array('arguments' => array('element' => NULL)),
'date_part_label_second' => $base + array('arguments' => array('element' => NULL)),
'date_part_label_ampm' => $base + array('arguments' => array('element' => NULL)),
'date_part_label_timezone' => $base + array('arguments' => array('element' => NULL)),
'date_views_filter_form' => $base + array(
'template' => 'date-views-filter-form',
'arguments' => array('form' => NULL)),
'date_calendar_day' => $base + array('arguments' => array('date' => NULL)),

'date_time_ago' => $base + array('arguments' => array('start_date' => NULL, 'end_date' => NULL, 'interval' => NULL)));
}
[/CODE]


That's all the references to am/pm in the module.
×

Success!

Help @News_design spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...