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

      Sentry launches MCP monitoring tool

      August 14, 2025

      10 Benefits of Hiring a React.js Development Company (2025–2026 Edition)

      August 13, 2025

      From Line To Layout: How Past Experiences Shape Your Design Career

      August 13, 2025

      Hire React.js Developers in the US: How to Choose the Right Team for Your Needs

      August 13, 2025

      GPT-5 in GitHub Copilot: How I built a game in 60 seconds

      August 14, 2025

      Q1 2025 Innovation Graph update: Bar chart races, data visualization on the rise, and key research

      August 14, 2025

      Setting the Stage: Inside the Process of Bringing Christian Fleming’s Work to Life in Print, Web, and 3D

      August 14, 2025

      On Accessibility Conformance, Design Systems, and CSS “Base” Units

      August 14, 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

      PHP 8.5.0 Beta 1 available for testing

      August 14, 2025
      Recent

      PHP 8.5.0 Beta 1 available for testing

      August 14, 2025

      Age Calculator using PHP

      August 14, 2025

      Laravel Boost is released

      August 13, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Ultimate ChatGPT-5 Prompt Guide: 52 Ideas for Any Task

      August 14, 2025
      Recent

      Ultimate ChatGPT-5 Prompt Guide: 52 Ideas for Any Task

      August 14, 2025

      8 Best Password Managers with 2FA Support (Free & Paid)

      August 14, 2025

      8 Best Antivirus Apps for Amazon Kindle Fire Tablets

      August 14, 2025
    • 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 ArticleUltimate ChatGPT-5 Prompt Guide: 52 Ideas for Any Task
    Next Article PHP 8.5.0 Beta 1 available for testing

    Related Posts

    Development

    PHP 8.5.0 Beta 1 available for testing

    August 14, 2025
    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-7774 – “Fortinet 5032 Session Credential Exposure Vulnerability”

    August 14, 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

    CVE-2025-48280 – AutomatorWP SQL Injection

    Common Vulnerabilities and Exposures (CVEs)

    CVE-2023-47356 – Mingyu Security Gateway Remote Command Execution Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-4838 – Kanwangzjm Funiture Open Redirect Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-6458 – Code-projects Online Hotel Reservation System SQL Injection Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    Highlights

    CVE-2025-46249 – Elementor Simple Calendar CSRF

    April 22, 2025

    CVE ID : CVE-2025-46249

    Published : April 22, 2025, 10:15 a.m. | 58 minutes ago

    Description : Cross-Site Request Forgery (CSRF) vulnerability in Michael Simple calendar for Elementor allows Cross Site Request Forgery. This issue affects Simple calendar for Elementor: from n/a through 1.6.4.

    Severity: 4.3 | MEDIUM

    Visit the link for more details, such as CVSS details, affected products, timeline, and more…

    CVE-2025-1706 – Adobe Flash Use-After-Free Vulnerability

    May 17, 2025

    CVE-2025-44612 – Tinxy WiFi Lock Controller Remote Information Disclosure

    May 30, 2025

    CVE-2025-6801 – Marvell QConvergeConsole Directory Traversal Arbitrary File Write Vulnerability

    July 7, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

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