PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
lazersam
Forum Contributor
Posts: 105 Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK
Post
by lazersam » Sun May 22, 2005 7:15 am
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.
lazersam
Forum Contributor
Posts: 105 Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK
Post
by lazersam » Sun May 22, 2005 7:27 am
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
phpScott
DevNet Resident
Posts: 1206 Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.
Post
by phpScott » Sun May 22, 2005 9:56 am
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.
cg111
Forum Newbie
Posts: 4 Joined: Sun May 22, 2005 8:49 am
Location: China
Post
by cg111 » Sun May 22, 2005 10:04 am
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
Last edited by
cg111 on Sun May 22, 2005 10:35 am, edited 1 time in total.
timvw
DevNet Master
Posts: 4897 Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium
Post
by timvw » Sun May 22, 2005 10:17 am
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);
lazersam
Forum Contributor
Posts: 105 Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK
Post
by lazersam » Sun May 22, 2005 11:33 am
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.
cg111
Forum Newbie
Posts: 4 Joined: Sun May 22, 2005 8:49 am
Location: China
Post
by cg111 » Sun May 22, 2005 12:03 pm
you can use "case" to rewrite "if....else...."
Skara
Forum Regular
Posts: 703 Joined: Sat Mar 12, 2005 7:13 pm
Location: US
Post
by Skara » Sun May 22, 2005 1:26 pm
case only works with specific values. He'd still need the if statements because of the < & >.