-1

I bet I can, but would it work like this?

function dutchDateNames($) {
        $day = explode('-', $date)[2];
        $dutchday = ($day < 10) ? substr($day, 1) : $day;

        $month = explode('-', $date)[1];
        if ($month == '01' . '02') {
            $dutchmonth = 'Januari' . 'Februari';
        }

        $dutchdate = $dutchday . ' ' . $dutchmonth . ' ' . explode('-', $date)[0];
        return $dutchdate
    }

So, if $month is 01, $dutchmonth should be Januari. If $month is 02, $dutchmonth should be Februari, and so on. I have the feeling I'm not doing this right?

LF00
  • 27,015
  • 29
  • 156
  • 295

5 Answers5

1

Like thatyou would not return any month cause you concatenate (mounth 0102 does not exist).

If i correctly understand your question i think an array will be better :

$month = explode('-', $date)[1]; //Ok you use this data like an index

$letterMonth = ['01' => 'Januari', '02' => 'Februari', ....]; // Create an array with correspondance number -> letter month

$dutchmonth = $letterMonth[$month]; Get the good month using your index
RomainP.
  • 21
  • 4
0

Try this:

Use elseif conditions

if ($month == '01') {
    $dutchmonth = 'Januari';
} elseif ($month == '02') {
    $dutchmonth = 'Februari';
} elseif ($month == '03') {
   $dutchmonth = '...'; 
} 
Dhara Parmar
  • 8,021
  • 1
  • 16
  • 27
0

Create lookup array and get value by key:

$month = '02';
$months = [
    '01' => 'Januari'
    '02' => 'Februari'
    // more months here
];
$dutchmonth = isset($months[$month])? $months[$month] : '';
echo $dutchmonth;
u_mulder
  • 54,101
  • 5
  • 48
  • 64
0

I think the proper way is to save the map a array. Demo

<?php
$array['01'] = 'Januari';
$array['02'] = 'Februari';
print_r($array);

echo $array[$month];
LF00
  • 27,015
  • 29
  • 156
  • 295
0

You may do any of these:

  1. if else

    if ($month == "01") {
        $dutchmonth = "Januari";
    } else if($month == "02"){
        $dutchmonth = "Februari";
    }
    
  2. switch

    switch($month) {
        case "01":
            $dutchmonth = "Januari";
            break;
        case "02":
            $dutchmonth = "Februari";
            break;
    }
    
  3. Using array

    $month_arr = array('01' => "Januari", '02' => "Februari");
    $dutchmonth = $month_arr[$month];
    

NOTE: To use multiple if conditions use logical operators && or ||

Yogesh Mistry
  • 2,082
  • 15
  • 19