Here’s a Age Calculator tool in PHP. This tool will take the user’s birthdate and calculate the exact age in years, months, and days.

1. PHP Code for Age Calculator Tool

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Age Calculator Tool to calculate age in years, months, and days.">
    <meta name="keywords" content="Age Calculator, PHP, Calculate Age, Date of Birth">
    <title>Age Calculator Tool</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 20px;
        }
        .container {
            max-width: 600px;
            margin: 0 auto;
            padding: 20px;
            background-color: #fff;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        h1 {
            text-align: center;
        }
        form {
            margin-top: 20px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input[type="date"], input[type="submit"] {
            width: 100%;
            padding: 10px;
            margin-bottom: 20px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        input[type="submit"] {
            background-color: #007bff;
            color: #fff;
            cursor: pointer;
        }
        .result {
            font-size: 18px;
            text-align: center;
            color: #333;
        }
        .error {
            color: red;
            text-align: center;
        }
    </style>
</head>
<body>

<div class="container">
    <h1>Age Calculator Tool</h1>
    <p>Calculate your exact age in years, months, and days based on your birthdate.</p>

    <form method="post">
        <label for="birthdate">Enter your birthdate:</label>
        <input type="date" id="birthdate" name="birthdate" required>

        <input type="submit" name="calculate_age" value="Calculate Age">
    </form>

    <?php
    if (isset($_POST['calculate_age'])) {
        $birthdate = $_POST['birthdate'];
        $current_date = date("Y-m-d");

        if (!empty($birthdate) && $birthdate <= $current_date) {
            $birthdate_object = new DateTime($birthdate);
            $current_date_object = new DateTime($current_date);

            // Calculate the difference
            $age_difference = $current_date_object->diff($birthdate_object);

            // Extract years, months, and days
            $years = $age_difference->y;
            $months = $age_difference->m;
            $days = $age_difference->d;

            echo "<div class='result'>You are $years years, $months months, and $days days old.</div>";
        } else {
            echo "<div class='error'>Please enter a valid birthdate.</div>";
        }
    }
    ?>
</div>

</body>
</html>

2. Integrating the Age Calculator into WordPress

You can integrate the Age Calculator into WordPress using two options:


Method 1: Create a Custom Plugin

Steps:
  1. Create a Plugin Folder:
  • Go to /wp-content/plugins/ and create a folder called age-calculator-tool.
  1. Create the Plugin File:
  • Inside the age-calculator-tool folder, create a file named age-calculator.php.
  • Add the following PHP code:
<?php
/*
Plugin Name: Age Calculator Tool
Description: A simple tool to calculate age in years, months, and days based on birthdate.
Version: 1.0
Author: Your Name
*/

// Function to display the Age Calculator Tool
function age_calculator_tool_shortcode() {
    ob_start(); // Start output buffering
    ?>

    <div class="container">
        <h1>Age Calculator Tool</h1>
        <p>Calculate your exact age in years, months, and days based on your birthdate.</p>

        <form method="post">
            <label for="birthdate">Enter your birthdate:</label>
            <input type="date" id="birthdate" name="birthdate" required>

            <input type="submit" name="calculate_age" value="Calculate Age">
        </form>

        <?php
        if (isset($_POST['calculate_age'])) {
            $birthdate = $_POST['birthdate'];
            $current_date = date("Y-m-d");

            if (!empty($birthdate) && $birthdate <= $current_date) {
                $birthdate_object = new DateTime($birthdate);
                $current_date_object = new DateTime($current_date);

                // Calculate the difference
                $age_difference = $current_date_object->diff($birthdate_object);

                // Extract years, months, and days
                $years = $age_difference->y;
                $months = $age_difference->m;
                $days = $age_difference->d;

                echo "<div class='result'>You are $years years, $months months, and $days days old.</div>";
            } else {
                echo "<div class='error'>Please enter a valid birthdate.</div>";
            }
        }
        ?>
    </div>

    <?php
    return ob_get_clean(); // Return the output
}

// Register the shortcode [age_calculator_tool]
add_shortcode('age_calculator_tool', 'age_calculator_tool_shortcode');
?>
  1. Activate the Plugin:
  • Go to the WordPress Admin Dashboard > Plugins and activate the Age Calculator Tool plugin.
  1. Add the Shortcode:
  • To display the tool on any post or page, use the shortcode:
   [age_calculator_tool]

Method 2: Using a PHP Code Execution Plugin

If you don’t want to create a custom plugin, you can use a PHP code execution plugin.

See also  how to create and design EMI calculator tool ?
Steps:
  1. Install a PHP Code Plugin:
  • Install and activate the “Insert PHP Code Snippet” plugin (or similar).
  1. Add the Age Calculator PHP Code:
  • Add the PHP code for the Age Calculator as a snippet.
  1. Use the Shortcode:
  • The snippet will generate a shortcode, like [insert_php_snippet id="1"].
  • Insert this shortcode in any post or page to display the calculator.

By following these steps, you can easily integrate the Age Calculator into your WordPress website!

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *