ProgramaTic Posted January 17, 2014 Share Posted January 17, 2014 (edited) 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 Edited January 17, 2014 by ProgramaTic Quote Link to comment https://forums.phpfreaks.com/topic/285454-switch-doesnt-working-correctly/ Share on other sites More sharing options...
jazzman1 Posted January 17, 2014 Share Posted January 17, 2014 Update 01-09 to 1-9 in your case statement. Quote Link to comment https://forums.phpfreaks.com/topic/285454-switch-doesnt-working-correctly/#findComment-1465602 Share on other sites More sharing options...
Ch0cu3r Posted January 17, 2014 Share Posted January 17, 2014 (edited) Leading zero's infront of numbers are ignored. Wrap your month numbers within quotes Edited January 17, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/285454-switch-doesnt-working-correctly/#findComment-1465604 Share on other sites More sharing options...
kicken Posted January 17, 2014 Share Posted January 17, 2014 (edited) 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 January 17, 2014 by kicken Quote Link to comment https://forums.phpfreaks.com/topic/285454-switch-doesnt-working-correctly/#findComment-1465605 Share on other sites More sharing options...
ProgramaTic Posted January 17, 2014 Author Share Posted January 17, 2014 Thanks all! Everything was solved correcly ^^ Quote Link to comment https://forums.phpfreaks.com/topic/285454-switch-doesnt-working-correctly/#findComment-1465606 Share on other sites More sharing options...
jazzman1 Posted January 17, 2014 Share Posted January 17, 2014 (edited) 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 January 17, 2014 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/285454-switch-doesnt-working-correctly/#findComment-1465607 Share on other sites More sharing options...
mac_gyver Posted January 17, 2014 Share Posted January 17, 2014 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; } } } Quote Link to comment https://forums.phpfreaks.com/topic/285454-switch-doesnt-working-correctly/#findComment-1465612 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.