Find Zodiac Sign from Date of Birth using PHP

0
804
Find Zodiac Sign from Date of Birth using PHP

Zodiac signs are a system of astrological signs that are based on the position of the sun relative to a constellation on a person’s birthday. There are twelve zodiac signs, each with its own set of personality traits, strengths, weaknesses, and other characteristics. The twelve signs are Aries, Taurus, Gemini, Cancer, Leo, Virgo, Libra, Scorpio, Sagittarius, Capricorn, Aquarius, and Pisces.

Aries (March 21-April 19) is the first sign of the zodiac and is characterized as a fire sign. Aries are known for their boldness, energy, and competitiveness.

Taurus (April 20-May 20) is an earth sign and is known for being reliable, practical, and grounded. They are often associated with sensuality and luxury.

Gemini (May 21-June 20) is an air sign and is known for being adaptable, curious, and sociable. They are often considered the “jack of all trades” and are skilled at communicating with others.

Cancer (June 21-July 22) is a water sign and is known for being emotional, sensitive, and nurturing. They are often associated with home and family and have a strong desire for security.

Leo (July 23-August 22) is a fire sign and is known for being confident, outgoing, and passionate. They are often associated with creativity and leadership.

Virgo (August 23-September 22) is an earth sign and is known for being detail-oriented, practical, and analytical. They are often associated with cleanliness and organization.

Libra (September 23-October 22) is an air sign and is known for being diplomatic, charming, and romantic. They are often associated with balance and harmony.

Scorpio (October 23-November 21) is a water sign and is known for being intense, passionate, and mysterious. They are often associated with transformation and deep emotions.

ALSO READ  Get Client IP Address using PHP

Sagittarius (November 22-December 21) is a fire sign and is known for being adventurous, optimistic, and philosophical. They are often associated with travel and exploring new horizons.

Capricorn (December 22-January 19) is an earth sign and is known for being disciplined, ambitious, and practical. They are often associated with hard work and success.

Aquarius (January 20-February 18) is an air sign and is known for being independent, eccentric, and humanitarian. They are often associated with innovation and social change.

Pisces (February 19-March 20) is a water sign and is known for being imaginative, intuitive, and compassionate. They are often associated with spirituality and creativity.

Follow this video for complete guidance :

Here’s a PHP function to get a user’s zodiac sign using their date of birth :

function getZodiacSign($birthdate){
  $signs = array(
    "Aquarius" => array("01-20", "02-18"),
    "Pisces" => array("02-19", "03-20"),
    "Aries" => array("03-21", "04-19"),
    "Taurus" => array("04-20", "05-20"),
    "Gemini" => array("05-21", "06-20"),
    "Cancer" => array("06-21", "07-22"),
    "Leo" => array("07-23", "08-22"),
    "Virgo" => array("08-23", "09-22"),
    "Libra" => array("09-23", "10-22"),
    "Scorpio" => array("10-23", "11-21"),
    "Sagittarius" => array("11-22", "12-21"),
    "Capricorn" => array("12-22", "01-19")
  );
  $month_day = date("m-d", strtotime($birthdate));
  $sign = "Capricorn";

  foreach ($signs as $key => $value) {
    if ($month_day >= $value[0] && $month_day <= $value[1]) {
      $sign = $key;
      break;
    }
  }
  return $sign;
}

In this example, we create an array of zodiac signs and their corresponding date ranges. We then use the date() function to extract the month and day from the user’s birthdate and store it in the $month_day variable. We loop through the $signs array and compare the user’s birthdate to each zodiac sign’s date range. If the user’s birthdate falls within a particular sign’s date range, we set the $sign variable to that sign and exit the loop. Finally, we return the user’s zodiac sign at the end of the function.

ALSO READ  Fibonacci series generator using PHP

Complete Source Code

<?php

function getZodiacSign($birthdate){
  $signs = array(
    "Aquarius" => array("01-20", "02-18"),
    "Pisces" => array("02-19", "03-20"),
    "Aries" => array("03-21", "04-19"),
    "Taurus" => array("04-20", "05-20"),
    "Gemini" => array("05-21", "06-20"),
    "Cancer" => array("06-21", "07-22"),
    "Leo" => array("07-23", "08-22"),
    "Virgo" => array("08-23", "09-22"),
    "Libra" => array("09-23", "10-22"),
    "Scorpio" => array("10-23", "11-21"),
    "Sagittarius" => array("11-22", "12-21"),
    "Capricorn" => array("12-22", "01-19")
  );
  $month_day = date("m-d", strtotime($birthdate));
  $sign = "";

  foreach ($signs as $key => $value) {
    if ($month_day >= $value[0] && $month_day <= $value[1]) {
      $sign = $key;
      break;
    }
  }
  return $sign;
}
?>

<style type="text/css">
  body{
    background: #214aa0;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .result{
    font-weight: bold;
    margin-top: 20px;
    background:#17af17;
    color:#fff;
    font-size:18px;
    padding:7px 20px;
  }
  input[type=date]{
    height:50px;
    width:250px;
  }
</style>

  <form id="zodiac-form" method="get">
    <div>
      <input type="date" name="dob" onchange="submitForm();" value="<?php echo (isset($_GET['dob']))?$_GET['dob']:'';?>">
    </div>
    <?php if(isset($_GET['dob'])){?>
      <div class="result">
        Your Zodiac Sign is <?php echo getZodiacSign($_GET['dob']);?>
      </div>
    <?php } ?>
  </form>


  <script type="text/javascript">
    function submitForm(){
      document.getElementById('zodiac-form').submit();
    }
  </script>

 

Comments are closed.