Australia's Leading Digital Marketing Experts. T. 1300 235 433  |  Aggregation Enquires Welcome

Find a Zodiac Sign by Birthdate with PHP

Here's some very simple code that'll determine a Zodiac sign given a birthdate. If I've screwed up on dates, let me know.

1
<?php 
2
/*
3
 Find a Zodiac Sign by Birthdate with PHP
4
 http://www.beliefmedia.com/code/php-snippets/zodiac-sign
5
*/
6
 
7
function beliefmedia_zodiac($birthdate) {
8
 
9
   $zodiac = '';
10
   list ($year, $month, $day) = explode ('-', $birthdate);
11
 
12
   if ( ( $month == 3 && $day > 20 ) || ( $month == 4 && $day < 20 ) ) { $zodiac = "Aries"; }
13
   elseif ( ( $month == 4 && $day > 19 ) || ( $month == 5 && $day < 21 ) ) { $zodiac = "Taurus"; }
14
   elseif ( ( $month == 5 && $day > 20 ) || ( $month == 6 && $day < 21 ) ) { $zodiac = "Gemini"; }
15
   elseif ( ( $month == 6 && $day > 20 ) || ( $month == 7 && $day < 23 ) ) { $zodiac = "Cancer"; }
16
   elseif ( ( $month == 7 && $day > 22 ) || ( $month == 8 && $day < 23 ) ) { $zodiac = "Leo"; }
17
   elseif ( ( $month == 8 && $day > 22 ) || ( $month == 9 && $day < 23 ) ) { $zodiac = "Virgo"; }
18
   elseif ( ( $month == 9 && $day > 22 ) || ( $month == 10 && $day < 23 ) ) { $zodiac = "Libra"; }
19
   elseif ( ( $month == 10 && $day > 22 ) || ( $month == 11 && $day < 22 ) ) { $zodiac = "Scorpio"; }
20
   elseif ( ( $month == 11 && $day > 21 ) || ( $month == 12 && $day < 22 ) ) { $zodiac = "Sagittarius"; }
21
   elseif ( ( $month == 12 && $day > 21 ) || ( $month == 1 && $day < 20 ) ) { $zodiac = "Capricorn"; }
22
   elseif ( ( $month == 1 && $day > 19 ) || ( $month == 2 && $day < 19 ) ) { $zodiac = "Aquarius"; }
23
   elseif ( ( $month == 2 && $day > 18 ) || ( $month == 3 && $day < 21 ) ) { $zodiac = "Pisces"; }
24
 
25
 return $zodiac;
26
}
27
[/php]
28
 
29
<strong>Usage:</strong>
30
[php]$birthdate = "2013-12-20";
31
echo beliefmedia_zodiac($birthdate);

When we posted this on Internoetics we had a number of comments - most of them relating to the date grouping.

Date Formats

For an 8-figure time-group (20131220), YYYYMMDD, remove the following line:

1
list ($year, $month, $day) = explode ("-", $birthdate);

... and use the following:

1
<?php 
2
$date = '20131220';
3
 
4
/* Format yyyymmdd */
5
preg_match('/([0-9]{4})([0-9]{2})([0-9]{2})/', $date, $matches);
6
 
7
$month = $matches['2'];
8
 $month = ltrim($month, '0');
9
$day = $matches['3'];
10
 $day = ltrim($day, '0');

You could also use PHP's str_split as an alternative the preg_match. Using str_split($date, 2); will split a string into an array with the values having a length of 2 characters. Remember, it's only the two-figure day and month that we're after.

Like this article?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on linkedin
Share on Linkdin
Share on pinterest
Share on Pinterest

Leave a comment