Added helper function to return date string

This commit is contained in:
Mark Nelson 2015-12-10 11:33:22 +08:00
parent d7907403e1
commit a9c01e2648

View file

@ -130,25 +130,7 @@ class customcert_element_date extends customcert_element_base {
// Ensure that a date has been set. // Ensure that a date has been set.
if (!empty($date)) { if (!empty($date)) {
switch ($dateformat) { parent::render_content($pdf, $this->get_date_format_string($date, $dateformat));
case 1:
$certificatedate = userdate($date, '%B %d, %Y');
break;
case 2:
$suffix = $this->get_ordinal_number_suffix(userdate($date, '%d'));
$certificatedate = userdate($date, '%B %d' . $suffix . ', %Y');
break;
case 3:
$certificatedate = userdate($date, '%d %B %Y');
break;
case 4:
$certificatedate = userdate($date, '%B %Y');
break;
default:
$certificatedate = userdate($date, get_string('strftimedate', 'langconfig'));
}
parent::render_content($pdf, $certificatedate);
} }
} }
@ -168,26 +150,7 @@ class customcert_element_date extends customcert_element_base {
$dateinfo = json_decode($this->element->data); $dateinfo = json_decode($this->element->data);
$dateformat = $dateinfo->dateformat; $dateformat = $dateinfo->dateformat;
$date = time(); return parent::render_html_content($this->get_date_format_string(time(), $dateformat));
switch ($dateformat) {
case 1:
$certificatedate = userdate($date, '%B %d, %Y');
break;
case 2:
$suffix = $this->get_ordinal_number_suffix(userdate($date, '%d'));
$certificatedate = userdate($date, '%B %d' . $suffix . ', %Y');
break;
case 3:
$certificatedate = userdate($date, '%d %B %Y');
break;
case 4:
$certificatedate = userdate($date, '%B %Y');
break;
default:
$certificatedate = userdate($date, get_string('strftimedate', 'langconfig'));
}
return parent::render_html_content($certificatedate);
} }
/** /**
@ -238,6 +201,35 @@ class customcert_element_date extends customcert_element_base {
return $dateformats; return $dateformats;
} }
/**
* Returns the date in a readable format.
*
* @param int $date
* @param string $dateformat
* @return string
*/
private function get_date_format_string($date, $dateformat) {
switch ($dateformat) {
case 1:
$certificatedate = userdate($date, '%B %d, %Y');
break;
case 2:
$suffix = $this->get_ordinal_number_suffix(userdate($date, '%d'));
$certificatedate = userdate($date, '%B %d' . $suffix . ', %Y');
break;
case 3:
$certificatedate = userdate($date, '%d %B %Y');
break;
case 4:
$certificatedate = userdate($date, '%B %Y');
break;
default:
$certificatedate = userdate($date, get_string('strftimedate', 'langconfig'));
}
return $certificatedate;
}
/** /**
* Helper function to return the suffix of the day of * Helper function to return the suffix of the day of
* the month, eg 'st' if it is the 1st of the month. * the month, eg 'st' if it is the 1st of the month.