3

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.

4 Answers 4

3

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.

7
  • i've tried with if too,but i dont know how to make it, when all cases are true,to execute the code lines 3 times,1 for each case Commented Sep 5, 2014 at 9:23
  • I have no clue what you mean with that. Commented Sep 5, 2014 at 9:41
  • read below and above.Sorry but i already said like 3-4 times,and i am tired to write it again Commented Sep 5, 2014 at 9:45
  • I can read what you write, but it just doesn't make sense that you would want to execute "the code lines 3 times per case". Why would you want to output 9 lines if all 3 conditions are true? Commented Sep 5, 2014 at 10:03
  • Obviously you didnt read well,i said i want to execute the code lines three times if all cases are true,in other words if all cases are true execute code lines from case 1 + code lines from case 2+ code lines from case 3.The others have undestood,as you can see Commented Sep 5, 2014 at 10:07
1

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>';   
    }  
6
  • and if i am using if,when all 3 cases are true,i need the lines code to execute 3 times,one for each case , your example would work in this situation??? Commented Sep 5, 2014 at 9:27
  • see execution goes one after another (line after line unless explicitly done otherwise). So if all three are true, it will print 'weekly<br>monthly<br>quarterly<br>'. If any of them are false, it will not print that particular condition.
    – Kamrul
    Commented Sep 5, 2014 at 9:32
  • well i'll try it and i'll return with an answer Commented Sep 5, 2014 at 9:33
  • @MaxGame oh that's what you wanted? i thought you wanted to stop the process as soon as one condition is false.
    – low_rents
    Commented Sep 5, 2014 at 10:11
  • What i wanted is,to execute only the lines of code which fulfill the condition.If 2 cases are true,then the lines of code after them,should trigger.Same if only 1 is true,or if all 3 are true.And it seems like your code works properly Commented Sep 5, 2014 at 10:19
1

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.

2
  • Thanks,i will try this too,and return with an answer,but even if it works,i must do it without function,just with if,switch,etc. Commented Sep 5, 2014 at 9:43
  • see my updated answer: added a new version which works without using a function.
    – low_rents
    Commented Sep 5, 2014 at 10:04
0

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.

4
  • OP citation: "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"
    – low_rents
    Commented Sep 5, 2014 at 9:17
  • Apparently I need to read questions better. Sorry about that.
    – Fluffeh
    Commented Sep 5, 2014 at 9:18
  • Edit made after some testing and validating my own surprise.
    – Fluffeh
    Commented Sep 5, 2014 at 9:25
  • I cant use this example,because after those cases,i need to make a pretty big script... Commented Sep 5, 2014 at 9:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.