Jump to content

Switch doesn't working correctly


ProgramaTic

Recommended Posts

Hi,

I'm making a code for determining the astrological sign of anyone.
The code receives a date in this format "YYYY-MM-DD" and strips that string in year, month and day.

So I've wrotten this function:

    //determinar signo
    public function Determinar(){
        switch ($this->month){
            case 01:
                if ($this->day <= 19){
                    $this->signo = "Capricornio";
                } else {
                    $this->signo = "Acuario";
                }
            break;
            case 02:
                if ($this->day <= 18){
                    $this->signo = "Acuario";
                } else {
                    $this->signo = "Piscis";
                }
            break;
            case 03:
                if ($this->day <= 20){
                    $this->signo = "Piscis";
                } else {
                    $this->signo = "Aries";
                }
            break;
            case 04:
                if ($this->day <= 20){
                    $this->signo = "Aries";
                } else {
                    $this->signo = "Tauro";
                }
            break;
            case 05:
                if ($this->day <= 20){
                    $this->signo = "Tauro";
                } else {
                    $this->signo = "Geminis";
                }
            break;
            case 06:
                if ($this->day <= 20){
                    $this->signo = "Geminis";
                } else {
                    $this->signo = "Cancer";
                }
            break;
            case 07:
                if ($this->day <= 20){
                    $this->signo = "Cancer";
                } else {
                    $this->signo = "Leo";
                }
            break;
            case 08:
                if ($this->day <= 21){
                    $this->signo = "Leo";
                } else {
                    $this->signo = "Virgo";
                }
            break;
            case 09:
                if ($this->day <= 22){
                    $this->signo = "Virgo";
                } else {
                    $this->signo = "Libra";
                }
            break;
            case 10:
                if ($this->day <= 22){
                    $this->signo = "Libra";
                } else {
                    $this->signo = "Escorpio";
                }
            break;
            case 11:
                if ($this->day <= 22){
                    $this->signo = "Escorpio";
                } else {
                    $this->signo = "Sagitario";
                }
            break;
            case 12:
                if ($this->day <= 20){
                    $this->signo = "Sagitario";
                } else {
                    $this->signo = "Capricornio";
                }
            break;
        }
    }

In this switch, cases 01 to 07 and 10 to 12 are working correctly.
But cases 08 and 09 doesn't works.

I can't find the problem/error, syntax seems to be correct bemused.gif

Edited by ProgramaTic
Link to comment
Share on other sites

Leading zero's infront of numbers are ignored.

Actually a leading 0 (in a number literal) causes the number to be treated as octal. In octal 08 and 09 are invalid numbers. The correct way to specify 8 and 9 would be 010 and 011. That is why the code failes on those two cases.

Edited by kicken
Link to comment
Share on other sites

I guess, he's trying to return the value of the month without leading zeros.

Something like:

<?php
date_default_timezone_set('UTC');

echo date("n");

//not

echo date("m");

EDIT: Ah....now I see this, after I've posted my first reply :)

 

 

In this switch, cases 01 to 07 and 10 to 12 are working correctly.
But cases 08 and 09 doesn't works.

 

Edited by jazzman1
Link to comment
Share on other sites

the same result (using English Zodiac names), with only 25 lines of code -

 //determinar signo
public function Determinar(){
    $signs[] = array('start'=>'03-21','end'=>'04-20','sign'=>'Aries');
    $signs[] = array('start'=>'04-21','end'=>'05-21','sign'=>'Taurus');
    $signs[] = array('start'=>'05-22','end'=>'06-21','sign'=>'Gemini');
    $signs[] = array('start'=>'06-22','end'=>'07-22','sign'=>'Cancer');
    $signs[] = array('start'=>'07-23','end'=>'08-22','sign'=>'Leo');
    $signs[] = array('start'=>'08-23','end'=>'09-23','sign'=>'Virgo');
    $signs[] = array('start'=>'09-24','end'=>'10-23','sign'=>'Libra');
    $signs[] = array('start'=>'10-24','end'=>'11-22','sign'=>'Scorpio');
    $signs[] = array('start'=>'11-23','end'=>'12-21','sign'=>'Sagittarius');
    $signs[] = array('start'=>'12-22','end'=>'12-31','sign'=>'Capricorn');
    $signs[] = array('start'=>'01-01','end'=>'01-20','sign'=>'Capricorn');
    $signs[] = array('start'=>'01-21','end'=>'02-19','sign'=>'Aquarius');
    $signs[] = array('start'=>'02-20','end'=>'03-20','sign'=>'Pisces');

    $date = sprintf("%02d-%02d",$this->month, $this->day);

    foreach($signs as $arr){
        if($date >= $arr['start'] && $date <= $arr['end']){
            $this->signo = $arr['sign'];
            break;
        }
    }
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.