5
\$\begingroup\$

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());
}
asked Mar 29, 2013 at 9:27
\$\endgroup\$

1 Answer 1

8
\$\begingroup\$

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
\$\endgroup\$
1
  • \$\begingroup\$ Thanks for your suggestion, it is indeed more clean this way. \$\endgroup\$ Commented Mar 29, 2013 at 12:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.