Petru Lebada
Petru Lebada

Reputation: 1682

How to proceed through all cases if they are true PHP

I want to know if there is a way to proceed through all three cases,if they are all true,but with using break,because as an example,if the first case is true,the second case is false and the third is also false,and i am not using break,it will procced trough all anyway.Change the strtotime with 6 October 2014,and you will see what i mean

$date = strtotime("1 October 2014"); 
switch($date) {  
     case (date('l', $date) == 'Monday'): //case 1: If the current day is Monday
      echo "weekly<br>";
     break;

     case (date('d', $date) == '01'):  //case 2: If the current day of the month is 1
      echo "monthly<br>";                       
     break;

    case ( ((date('n') % 3) == '1') && (date('d') == '01') ): //case 3: If a quart of the year has just passed,and we are in the first day of a new quart
      echo 'quarterly<br>';     
    break;   

}

Any suggestion??? And if it is not possible with switch,how should i make it with if,to execute the code line 3 times,one for each case.

Upvotes: 3

Views: 256

Answers (4)

Kamrul
Kamrul

Reputation: 7311

try

$date = strtotime("1 October 2014"); 

     if (date('l', $date) == 'Monday'){ //case 1: If the current day is Monday
      echo "weekly<br>";
     }
     if (date('d', $date) == '01'){  //case 2: If the current day of the month is 1
      echo "monthly<br>";                       
     }
    if ( ((date('n', $date) % 3) == '1') && (date('d', $date) == '01') ){ //case 3: If a quart of the year has just passed,and we are in the first day of a new quart
      echo 'quarterly<br>';   
    }  

Upvotes: 1

low_rents
low_rents

Reputation: 4481

UPDATE: solution without the use of a function:

$date = strtotime("1 September 2014");
$continue = true;

if(date('l', $date) == 'Monday'): //case 1: If the current day is Monday
    echo "weekly<br>";
else:
    $continue = false;
endif;

if(date('d', $date) == '01' && $continue):  //case 2: If the current day of the month is 1
    echo "monthly<br>";
else:
    $continue = false;
endif;

if( ((date('n') % 3) == '1') && (date('d') == '01') && $continue ): //case 3: If a quart of the year has just passed,and we are in the first day of a new quart
    echo 'quarterly<br>'; 
endif;


OLD VERSION (with the use of a function): i just coded a tiny function for you that should exactly fit your needs:

function sequencer($date_string)
{
    $date = strtotime($date_string);

    if(date('l', $date) == 'Monday'): //case 1: If the current day is Monday
        echo "weekly<br>";
    else:
        return;
    endif;

    if(date('d', $date) == '01'):  //case 2: If the current day of the month is 1
        echo "monthly<br>";
    else:
        return;
    endif;

    if( ((date('n') % 3) == '1') && (date('d') == '01') ): //case 3: If a quart of the year has just passed,and we are in the first day of a new quart
        echo 'quarterly<br>'; 
    else:
        return;
    endif;
}

sequencer("1 October 2014");

as you can see above, just call it with the date string, the strotime() is also done inside the function.

Upvotes: 1

Fluffeh
Fluffeh

Reputation: 33542

Sorry about the first answer - I never tried to do what you are doing and I apparently can't read questions properly.

Having said that, you can actually execute an echo statement with a condition to set what you want to display:

<?php

    $date = strtotime("6 October 2014");

    echo (date('l', $date) == 'Monday')?"Weekly<br>":"";
    echo (date('d', $date) == '01') == 'Monday')?"Monthly<br>":"";
    echo ( ((date('n') % 3) == '1') && (date('d') == '01') ) == 'Monday')?"Quarterly<br>":"";

?>

The above will output your desired behaviour for example.

Upvotes: 0

Niels Keurentjes
Niels Keurentjes

Reputation: 41968

This is just not how switch works - it is intended to execute a single line of execution based on defined conditions - hence a 'switch'. Replace it with separate if statements.

Upvotes: 3

Related Questions