How to make a day finder in php

Day finder program shows us day name of the week from numeric value such as: 1,2,3,4.. upto 7.

If we consider Saturday=1, Sunday=2, Monday=3, Tuesday=4, Wednesday=5, Thursday=6 & Friday=7.

Then we can simply make a day finder program in php which shows us the day name from the day number.

We can make the day finder easily by using array and switch in php.

Firstly, we will create an array of days:

$days = array(1=>"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday");

We have put ‘1=>’ to index the array from number 1. If you don’t use it, then the array will start from number 0 & consider Saturday=1,…,Friday=6 respectively.

We will create a variable and assign our desired number to output the name of the desired day. Let the desired day be friday and we will assign number 7 to output friday.

 

$day=7;

Now we will make a switch statement to output the day name from the given value.


switch($day)
    {
        case 1: echo $days[1];
            break;
        case 2: echo $days[2];
            break;
        case 3: echo $days[3];
            break;
        case 4: echo $days[4];
            break;
        case 5: echo $days[5];
            break;
        case 6: echo $days[6];
            break;
         case 7: echo $days[7];
            break;
        default: echo "Invalid input! Please enter day number between 1-7";
    }

If all continues well, then the output will Friday.