calculate zodiac / star sign in PHP for mysql date

chris (2006-06-06 11:17:25)
11201 views
1 replies
I needed to convert a date of birth into a star sign - it's pretty simple, but rather than code it all out myself I decided to search for "calculate star sign in PHP" and various permutations thereof... all I found was a load of trashy astrology sites, so I coded it myself. It took about 20 minutes and might not be the most elegant solution - as usual there are a dozen ways of doing this, but here's my solution anyway:
<?php
// the function is used like this:
// get the date out from the mysql database and pass it to the getsign function below

$dob=$db->query("select us_dob from users where us_id=$userid");
$sign=getsign($dob);

print "nstar sign is $signn";

// this is the getsign function itself

function getsign($date){
     list($year,$month,$day)=explode("-",$date);
     if(($month==1 && $day>20)||($month==2 && $day<20)){
          return "Aquarius";
     }else if(($month==2 && $day>18 )||($month==3 && $day<21)){
          return "Pisces";
     }else if(($month==3 && $day>20)||($month==4 && $day<21)){
          return "Aries";
     }else if(($month==4 && $day>20)||($month==5 && $day<22)){
          return "Taurus";
     }else if(($month==5 && $day>21)||($month==6 && $day<22)){
          return "Gemini";
     }else if(($month==6 && $day>21)||($month==7 && $day<24)){
          return "Cancer";
     }else if(($month==7 && $day>23)||($month==8 && $day<24)){
          return "Leo";
     }else if(($month==8 && $day>23)||($month==9 && $day<24)){
          return "Virgo";
     }else if(($month==9 && $day>23)||($month==10 && $day<24)){
          return "Libra";
     }else if(($month==10 && $day>23)||($month==11 && $day<23)){
          return "Scorpio";
     }else if(($month==11 && $day>22)||($month==12 && $day<23)){
          return "Sagittarius";
     }else if(($month==12 && $day>22)||($month==1 && $day<21)){
          return "Capricorn";
     }
}
?>

This function doesn't use any PHP specific functions for date calculations, so it would convert easily into perl, java, C# ... etc

christo
comment
Raptor
2010-02-03 03:14:54

Suggestions

suggestions:
1. better replace "else if" to "elseif"
2. function signature may change to accept $day and $month only, as $year is not related.
reply iconedit reply