JDate/toFormat
From Joomla! Documentation
< API16:JDate
The "API16" namespace is an archived namespace. This page contains information for a Joomla! version which is no longer supported. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.
Description[edit]
Gets the date in a specific format
<! removed transcluded page call, red link never existed >
Syntax[edit]
toFormat($format= '%Y-%m-%d%H:%M:%S', $local=true)
Parameter Name | Default Value | Description |
---|---|---|
$format | '%Y-%m-%d%H:%M:%S' | The date format specification string (see ) |
$local | true | True to return the date string in the local time zone, false to return it in GMT. |
Returns[edit]
string The date as a formatted string.
Defined in[edit]
libraries/joomla/utilities/date.php
Importing[edit]
jimport( 'joomla.utilities.date' );
Source Body[edit]
public function toFormat($format = '%Y-%m-%d %H:%M:%S', $local = true)
{
// Set time zone to GMT as strftime formats according locale setting.
date_default_timezone_set('GMT');
// Generate the timestamp.
$time = (int) parent::format('U');
// If the returned time should be local add the GMT offset.
if ($local) {
$time += $this->getOffsetFromGMT();
}
// Manually modify the month and day strings in the format.
if (strpos($format, '%a') !== false) {
$format = str_replace('%a', $this->_dayToString(date('w', $time), true), $format);
}
if (strpos($format, '%A') !== false) {
$format = str_replace('%A', $this->_dayToString(date('w', $time)), $format);
}
if (strpos($format, '%b') !== false) {
$format = str_replace('%b', $this->_monthToString(date('n', $time), true), $format);
}
if (strpos($format, '%B') !== false) {
$format = str_replace('%B', $this->_monthToString(date('n', $time)), $format);
}
// Generate the formatted string.
$date = strftime($format, $time);
return $date;
}
<! removed transcluded page call, red link never existed >
Examples[edit]
Code Examples[edit]