Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Error’d: Pickup Sticklers

      September 27, 2025

      From Prompt To Partner: Designing Your Custom AI Assistant

      September 27, 2025

      Microsoft unveils reimagined Marketplace for cloud solutions, AI apps, and more

      September 27, 2025

      Design Dialects: Breaking the Rules, Not the System

      September 27, 2025

      Building personal apps with open source and AI

      September 12, 2025

      What Can We Actually Do With corner-shape?

      September 12, 2025

      Craft, Clarity, and Care: The Story and Work of Mengchu Yao

      September 12, 2025

      Cailabs secures €57M to accelerate growth and industrial scale-up

      September 12, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Using phpinfo() to Debug Common and Not-so-Common PHP Errors and Warnings

      September 28, 2025
      Recent

      Using phpinfo() to Debug Common and Not-so-Common PHP Errors and Warnings

      September 28, 2025

      Mastering PHP File Uploads: A Guide to php.ini Settings and Code Examples

      September 28, 2025

      The first browser with JavaScript landed 30 years ago

      September 27, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured
      Recent
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Age Calculator using PHP

    Age Calculator using PHP

    August 14, 2025

    Simple Age Calculator using PHP. In this script, the user will enter their date of birth. The PHP function will calculate the age in years, months, and days using DateTime and diff() function and show the outcome on the page.

    Get the Date of Birth from the form

    $dob = $_POST['dob'];

    The date will be in the format YYYY-MM-DD.

    Create DateTime objects

    $dobObject = new DateTime($dob); 
    $today = new DateTime();

    $dobObject → Creates a DateTime object from the entered date of birth.

    $today → Creates a DateTime object for the current date (today).

    Calculate the difference

    $ageInterval = $today->diff($dobObject);

    The diff() method compares two DateTime objects and returns a DateInterval object.

    Store the age in an array

    $age = [
        'years' => $ageInterval->y,
        'months' => $ageInterval->m,
        'days' => $ageInterval->d
    ];

    $ageInterval->y → Number of years between DOB and today.

    $ageInterval->m → Remaining months after subtracting years.

    $ageInterval->d → Remaining days after subtracting months.

    Here is the full code that we have written during this tutorial:

    <?php
    if (isset($_POST['calculate'])) {
        $dob = $_POST['dob'];
        $dobObject = new DateTime($dob); 
        $today = new DateTime();
    
        $ageInterval = $today->diff($dobObject); 
    
        $age = [
            'years' => $ageInterval->y,
            'months' => $ageInterval->m,
            'days' => $ageInterval->d
        ];
    }
    ?>
    
    <!DOCTYPE html>
    <html>
    <head>
        <title>PHP Age Calculator</title>
        <style>
            body {
                font-family: Arial, sans-serif;
                background: linear-gradient(120deg, #6a11cb 0%, #2575fc 100%);
                color: #333;
                margin: 0;
                padding: 0;
            }
            .container {
                max-width: 450px;
                background: #fff;
                padding: 30px;
                margin: 80px auto;
                border-radius: 12px;
                box-shadow: 0 8px 20px rgba(0,0,0,0.15);
                text-align: center;
            }
            h2 {
                color: #2575fc;
                margin-bottom: 20px;
            }
            label {
                font-weight: bold;
                display: block;
                margin-bottom: 10px;
            }
            input[type="date"] {
                padding: 10px;
                border: 1px solid #ccc;
                border-radius: 6px;
                width: 100%;
                margin-bottom: 20px;
                font-size: 16px;
            }
            button {
                background: #2575fc;
                color: white;
                padding: 10px 20px;
                border: none;
                font-size: 16px;
                border-radius: 6px;
                cursor: pointer;
                transition: 0.3s;
            }
            button:hover {
                background: #1e5ed8;
            }
            .result {
                margin-top: 20px;
                padding: 15px;
                background: #f4f6ff;
                border-left: 5px solid #2575fc;
                border-radius: 6px;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <h2>PHP Age Calculator</h2>
            <form method="post">
                <label>Enter Date of Birth:</label>
                <input type="date" name="dob" required>
                <button type="submit" name="calculate">Calculate Age</button>
            </form>
    
            <?php if (isset($age)) { ?>
                <div class="result">
                    <h3>Your Age is:</h3>
                    <p><strong><?php echo $age['years']; ?></strong> Years, 
                       <strong><?php echo $age['months']; ?></strong> Months, 
                       <strong><?php echo $age['days']; ?></strong> Days</p>
                </div>
            <?php } ?>
        </div>
    </body>
    </html>

    View Demo


    View Demo
    Age Calculator using PHP
    Size: 1.84 KB
    Version: V 1.0
    Download Now!

    The post Age Calculator using PHP appeared first on PHPGurukul.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleSentry launches MCP monitoring tool
    Next Article PHP 8.5.0 Beta 1 available for testing

    Related Posts

    Development

    Using phpinfo() to Debug Common and Not-so-Common PHP Errors and Warnings

    September 28, 2025
    Development

    Mastering PHP File Uploads: A Guide to php.ini Settings and Code Examples

    September 28, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    Sudo-rs make me a sandwich, hold the buffer overflows

    Security

    Agencies Assemble

    Web Development

    Anthropic beats OpenAI as the top LLM provider for business – and it’s not even close

    News & Updates

    ShadowSilk Hits 35 Organizations in Central Asia and APAC Using Telegram Bots

    Development

    Highlights

    AI Is Flipping UX Upside Down

    May 23, 2025

    The article explores the fundamental shift in UX as AI-first systems minimize the role of…

    UX Redesign: 7 Signs Your Product Might Be Due for One

    April 15, 2025

    Will you be the boss of your own AI workforce?

    April 25, 2025

    From GenAI Demos to Production: Why Structured Workflows Are Essential

    April 25, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.