Page 1 of 1

Compare Dates

Posted: Sun May 22, 2005 7:15 am
by lazersam
Hi
I am trying to calculate star-signs given just the month and day.

So far I have this... but it doesn't work for Capricorn.

Code: Select all

$ss_date = "12/24";
			echo "ss_date=$ss_date<br>";
			
			if ($ss_date >= "03/21" and $ss_date <= "04/20") {
				$un_star_sign = "Aries";				    
				}
			if ($ss_date >= "04/21" and $ss_date <= "05/21") {
				$un_star_sign = "Taurus";				    
				}
			if ($ss_date >= "05/22" and $ss_date <= "06/21") {
				$un_star_sign = "Gemini";				    
				}
			if ($ss_date >= "06/22" and $ss_date <= "07/23") {
				$un_star_sign = "Cancer";				    
				}
			if ($ss_date >= "07/24" and $ss_date <= "08/23") {
				$un_star_sign = "Leo";				    
				}
			if ($ss_date >= "08/24" and $ss_date <= "09/23") {
				$un_star_sign = "Virgo";				    
				}
			if ($ss_date >= "09/24" and $ss_date <= "10/23") {
				$un_star_sign = "Libra";				    
				}
			if ($ss_date >= "10/24" and $ss_date <= "11/22") {
				$un_star_sign = "Scorpio";				    
				}
			if ($ss_date >= "11/23" and $ss_date <= "12/22") {
				$un_star_sign = "Sagittarius";				    
				}
			if ($ss_date >= "12/23" and $ss_date <= "01/20") {
				$un_star_sign = "Capricorn";			    
				}
			if ($ss_date >= "01/21" and $ss_date <= "02/19") {
				$un_star_sign = "Aquarius";				    
				}
			if ($ss_date >= "02/20" and $ss_date <= "03/20") {
				$un_star_sign = "Pisces";				    
				}
			    
			echo "un_star_sign=$un_star_sign<br>";
I think I should be using date() or something like that!! Can anyone point me in the right direction?

Larry.

Posted: Sun May 22, 2005 7:27 am
by lazersam
I suppose I could test for a null result at the end of the search and then default to Capricorn - but that is not the most tidy way of doing it :)

Code: Select all

if (!$un_star_sign) {
    $un_star_sign = "Capricorn";
}

Larry

Posted: Sun May 22, 2005 9:56 am
by phpScott
first of all i would change all your 'and' to '&&'.

not sure why but I think php might be interpeting your ss_date as a value and not necessarliy as 12/24 but 0.5.

one way to solve this would be to remove the / from ss_date and all your comparisions and reorder your if statements from smallest date to larges date.

also(can't quite remeber if it works) but to use a switch statment with the comparisions in them as it will make your code easier to read.

i,think you can use mktime.

Posted: Sun May 22, 2005 10:04 am
by cg111
my english is so bad,so i can't explan why i do,the next is my suggest:

Code: Select all

<?php
$ss_date =date("m-d",mktime(0,0,0,1,26));
            echo "ss_date=$ss_date<br>";
            
            if ($ss_date >= "03-21" and $ss_date <= "04-20") {
                $un_star_sign = "Aries";                    
                }
            if ($ss_date >= "04-21" and $ss_date <= "05-21") {
                $un_star_sign = "Taurus";                    
                }
            if ($ss_date >= "05-22" and $ss_date <= "06-21") {
                $un_star_sign = "Gemini";                    
                }
            if ($ss_date >= "06-22" and $ss_date <= "07-23") {
                $un_star_sign = "Cancer";                    
                }
            if ($ss_date >= "07-24" and $ss_date <= "08-23") {
                $un_star_sign = "Leo";                    
                }
            if ($ss_date >= "08-24" and $ss_date <= "09-23") {
                $un_star_sign = "Virgo";                    
                }
            if ($ss_date >= "09-24" and $ss_date <= "10-23") {
                $un_star_sign = "Libra";                    
                }
            if ($ss_date >= "10-24" and $ss_date <= "11-22") {
                $un_star_sign = "Scorpio";                    
                }
            if ($ss_date >= "11-23" and $ss_date <= "12-22") {
                $un_star_sign = "Sagittarius";                    
                }
            if ($ss_date >= "12-23" and $ss_date <= "01-20") {
                $un_star_sign = "Capricorn";                
                }
            if ($ss_date >= "01-21" and $ss_date <= "02-19") {
                $un_star_sign = "Aquarius";                    
                }
            if ($ss_date >= "02-20" and $ss_date <= "03-20") {
                $un_star_sign = "Pisces";                    
                }
                
            echo "un_star_sign=$un_star_sign <br>"
?>
good luck

Posted: Sun May 22, 2005 10:17 am
by timvw
untested... but should be enough to get you inspired...

Code: Select all

function starsign($month, $day)
{
  if (($month == 3 && $day > 20) || ($month == 4 && $day < 21))
  {
    return "Aries";
  }
  else if ($month == 4 && $day > 21) || ($month == 5 && $day < 21))
  {
    return "Taurus";
  }

  ...
}


$ss_date = "12/24";
list($month, $day) = split('/', $ss_date);
echo starsign($month, $day);

Posted: Sun May 22, 2005 11:33 am
by lazersam
Thanks all for your help... much appreciated :)

Here's the working version inspired by timvw...

Code: Select all

$ss_date = "12/24";
			 list($month, $day) = split('/', $ss_date);
			 echo starsign($month, $day);
			
			
			
			
			
		function starsign($month, $day){
			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";
			 }
			 else if (($month == 1 && $day > 20) || ($month == 2 && $day < 20))
			 { 
			 return "Aquarius";
			 }
			 else if (($month == 2 && $day >19) || ($month == 3 && $day < 21))
			 { 
			 return "Pisces";
			 }
			 
		}
Regards

Larry.

Posted: Sun May 22, 2005 12:03 pm
by cg111
you can use "case" to rewrite "if....else...."

Posted: Sun May 22, 2005 1:26 pm
by Skara
case only works with specific values. He'd still need the if statements because of the < & >.