\$\begingroup\$
\$\endgroup\$
I am wondering what would be an efficient way to simplify this very simple PHP method and I really lack inspiration:
public function formatToFullDate($countryCode, $hideTime = false)
{
$format = '';
if($hideTime === true)
{
switch($countryCode)
{
case 'us':
case 'ca':
$format = '%A %e %B %Y';
break;
case 'de':
$format = '%A, den %e. %B %Y';
break;
default:
$format = '%A %e %B %Y';
}
}
else
{
switch($countryCode)
{
case 'us':
case 'ca':
$format = '%A %e %B %Y - %I:%M %P';
break;
case 'de':
$format = '%A, den %e. %B %Y - %H:%M';
break;
default:
$format = '%A %e %B %Y - %H:%M';
}
}
return gmstrftime($format, $this->getTimestamp());
}
cellovercellover
1 Answer 1
\$\begingroup\$
\$\endgroup\$
1
There is still some redundant code in there(' - %H:%M' and '%A %e %B %Y'). You could put these in variabels, but i like it so more. Sory for my bad English.
public function formatToFullDate($countryCode, $hideTime = false)
{
$format = '';
$time_format = '';
switch($countryCode)
{
case 'us':
case 'ca':
$time_format = ' - %I:%M %P';
$date_format = '%A %e %B %Y';
break;
case 'de':
$time_format = ' - %H:%M';
$date_format = '%A, den %e. %B %Y';
break;
default:
$date_format = '%A %e %B %Y';
$time_format = ' - %H:%M';
}
if($hideTime)$time_format = '';
return gmstrftime($date_format.$time_format, $this->getTimestamp());
}
answered Mar 29, 2013 at 11:48
-
\$\begingroup\$ Thanks for your suggestion, it is indeed more clean this way. \$\endgroup\$cellover– cellover2013年03月29日 12:45:00 +00:00Commented Mar 29, 2013 at 12:45
lang-php