Navigation

    ask avan logo
    • Register
    • Login
    • Search
    • Categories
    • Unsolved
    • Solved

    Switch statement php

    PHP
    php
    2
    6
    70
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • MPG Radio
      MPG Radio last edited by avan

      How would I say in this switch statement Pisces displays at X number of days in January and Aries at some other day in the same month? Dahnus is default for January and should disappear when replaced by the others on the specified days.

      There will be 24 months in this switch all having replacement switch definitions.

      <?php
      // date command to find out the day of the week
      $date = date('F');
      // our switch statement will assess which day of the week it is and
      // assign our $content as assigned.
      switch ($date) {
        case 'Jan':
          $content = "Dahnus";
      //$content = " Pisces";
      //$content = " Aries";
          break;
        case 'Feb':
          $content = "Aquarius";
      //$content = " Virgo";
      //$content = " Kanya";
      
          break;
        
      }
      
      // display our content regardless of day 
      echo $content;
      ?>
      Reply Quote 0
        1 Reply Last reply

      • MPG Radio
        MPG Radio last edited by

        Adding the solution I had come up with.

        <?php
        // get the date in an easy to compare format: Jan 1st is 1 01, Dec 31st 12 31
        $date = date('n') * 100 + date('j');
        
        if($date < 104) {
                        $content = "Sagittarius";
        } elseif($date < 105) {
        				//January
                        $content = "Dahnus";
        } elseif($date < 118) {
        				//January
                        $content = "Capricornus";
        } elseif($date < 201) {
        				//February
                        $content = "Makra";
        } elseif($date < 215) {
        				//February
                        $content = "Aquarius";
        } elseif($date < 218) {
        				//February
                        $content = "Kumbha";
        } elseif($date < 305) {
                        $content = "Pisces";
        } elseif($date < 801) {
        				//August
                        $content = "Leo";
        } elseif($date < 831) {
        				//september
                        $content = "Simha";
        } elseif($date < 918) {
        				//september
                        $content = "virgo";
        } elseif($date < 1001) {
        				//september
                        $content = "virgo";
        } elseif($date < 1016) {
                        //october
        				$content = "Kanya";
        } elseif($date < 1017) {
                        //october
        				$content = "Libra";
        } elseif($date < 1105) {
        				//november
                        $content = "Tula";
        } elseif($date < 1124) {
        				//november
                        $content = "Scorpius";
        } elseif($date < 1208) {
                        $content = "Vrishika";
        } elseif($date < 1228) {
                        $content = "Sagittarius";
        } else {
                        $content = "Sagittarius";
        }
        echo $content;
        ?>
        
        Reply Quote 0
          1 Reply Last reply

        • avan
          avan last edited by

          Hey again @MPG-Radio

          I am afraid I am still confused on what is being asked. Do you mind clarifying?

          Reply Quote 0
            1 Reply Last reply

          • avan
            avan last edited by

            @MPG-Radio

            I took a look at your problem again. If I understood you correctly couldn't you just use an if statement inside each switch case? So depending on the day within the month return a certain value. But again, I am not sure if I understood your question correctly.

            Reply Quote 0
              1 Reply Last reply

            • MPG Radio
              MPG Radio last edited by

              Yes, a if statement sounds like the option, I think something like this....
              Here’s how I think I might approach it:

              // get the date in an easy to compare format: Jan 1st is 101, Dec 31st 1231
              $date = date('n') * 100 + date('j');

              if($date < 124) {
              $content = "between jan 1 and jan 23;
              } elseif($date < 215) {
              $content = "between jan 24 and feb 15";
              }

              // ... repeat for each grouping I want?

              Reply Quote 0
                1 Reply Last reply

              • avan
                avan last edited by

                What about an if statement within each switch case?

                switch ($date) {
                  case 'Jan':
                    $content = "Dahnus";
                    if(CONDITION HERE)  {  
                           EXECUTE CODE HERE
                      }
                //$content = " Pisces";
                //$content = " Aries";
                    break;
                  case 'Feb':
                
                Reply Quote 0
                  1 Reply Last reply

                • MPG Radio
                  MPG Radio last edited by

                  Adding the solution I had come up with.

                  <?php
                  // get the date in an easy to compare format: Jan 1st is 1 01, Dec 31st 12 31
                  $date = date('n') * 100 + date('j');
                  
                  if($date < 104) {
                                  $content = "Sagittarius";
                  } elseif($date < 105) {
                  				//January
                                  $content = "Dahnus";
                  } elseif($date < 118) {
                  				//January
                                  $content = "Capricornus";
                  } elseif($date < 201) {
                  				//February
                                  $content = "Makra";
                  } elseif($date < 215) {
                  				//February
                                  $content = "Aquarius";
                  } elseif($date < 218) {
                  				//February
                                  $content = "Kumbha";
                  } elseif($date < 305) {
                                  $content = "Pisces";
                  } elseif($date < 801) {
                  				//August
                                  $content = "Leo";
                  } elseif($date < 831) {
                  				//september
                                  $content = "Simha";
                  } elseif($date < 918) {
                  				//september
                                  $content = "virgo";
                  } elseif($date < 1001) {
                  				//september
                                  $content = "virgo";
                  } elseif($date < 1016) {
                                  //october
                  				$content = "Kanya";
                  } elseif($date < 1017) {
                                  //october
                  				$content = "Libra";
                  } elseif($date < 1105) {
                  				//november
                                  $content = "Tula";
                  } elseif($date < 1124) {
                  				//november
                                  $content = "Scorpius";
                  } elseif($date < 1208) {
                                  $content = "Vrishika";
                  } elseif($date < 1228) {
                                  $content = "Sagittarius";
                  } else {
                                  $content = "Sagittarius";
                  }
                  echo $content;
                  ?>
                  
                  Reply Quote 0
                    1 Reply Last reply

                  • First post
                    Last post