0

Persian day names are different than English, and also week starts Saturday. So I have this weekday name and number conversion function:

function getDayNames($day, $shorten = false, $len = 1, $numeric = false)
{
    $ret = '';
    switch ( strtolower($day) ) {
        case 'sat': case 'saturday':  $ret = 'شنبه'; $n = 1; break;
        case 'sun': case 'sunday':    $ret = 'یکشنبه'; $n = 2; break;
        case 'mon': case 'monday':    $ret = 'دوشنبه'; $n = 3; break;
        case 'tue': case 'tuesday':   $ret = 'سه شنبه'; $n = 4; break;
        case 'wed': case 'wednesday': $ret = 'چهارشنبه'; $n = 5; break;
        case 'thu': case 'thursday':  $ret = 'پنجشنبه'; $n = 6; break;
        case 'fri': case 'friday':    $ret = 'جمعه'; $n = 7; break;
    }
    return ($numeric) ? $n : (($shorten) ? substr($ret, 0, $len) : $ret);
}

and I wanna know if there is a better way to rewrite this. Using switch is not ideal.

Thanks in advance.

Sallar
  • 730
  • 1
  • 9
  • 23
  • 1
    Set up some hash dictionary (ie a PHP array) with the english strings as keys and the farsi ones as values. – mvw Sep 02 '13 at 13:28
  • 1
    I would use a hash table (i.e. an associative array) as a static variable, and look up the values based on the first three letters on the input. But there's actually little-to-no gain from doing this, it's certainly not a meaningful performance difference, so you should use the most readable version. But that `substr()` is *highly* unlikely to be what you want - in PHP a string is a byte sequence, and you will almost certainly cut characters in half. Use `mb_substr()` instead. – DaveRandom Sep 02 '13 at 13:29
  • @mvw The problem is I have two keys I wanna convert, and I don't want to set up two hash dictionaries with the same values. 'sat' and 'saturday' should point to the same value. – Sallar Sep 02 '13 at 13:30
  • I would see if I can use "assignment by reference" in that case. – mvw Sep 02 '13 at 13:37
  • This function is doing far too much, why not use 3 separate functions? – Anthony Sterling Sep 02 '13 at 13:43

1 Answers1

2

Possibly arrayify it:

function getDayNames($day, $shorten = false, $len = 1, $numeric = false) {
    $days = array(
        'sat' => 'شنبه',
        'sun' => 'یکشنبه',
        'mon' => 'دوشنبه',
        'tue' => 'سه شنبه',
        'wed' => 'چهارشنبه',
        'thu' => 'پنجشنبه',
        'fri' => 'جمعه'
    );

    $key = substr(strtolower($day), 0, 3);
    if (array_key_exists($key, $days) {
        $ret = $days[$key];
    } else {
        throw new Expection('Day ' . $day . ' not found!');
    }

    return ($numeric) ? (array_search($key, array_keys($days)) + 1) : (($shorten) ? substr($ret, 0, $len) : $ret);
}
Connor Peet
  • 6,065
  • 3
  • 22
  • 32
  • Thanks, but what about the numeric value which is week day number? How should I return "1" if the matched item is 'sat' ans so on. – Sallar Sep 02 '13 at 13:32
  • This has a syntax error here: `if (array_key_exists($key, $days)` but this is it. Thanks:) – Sallar Sep 02 '13 at 13:44