skip to Main Content

The issue is the cPanel Error message.

I have a PHP script that auto shows (Sara Smile Is * Years of Age), and I have in (.htaccess) the line (AddType application/x-httpd-php .html), and that runs properly, however cPanel is giving this Error Message: Expected tag name. Got ‘?’ instead. (HTML doesn’t support processing instructions).

<?php
$bday = new DateTime('11.4.2010'); // Persons Date of Birth
$today = new Datetime(date('m.d.y'));
$diff = $today->diff($bday);
printf(' %d ', $diff->y, $diff->m, $diff->d);
printf("n");
?>

Is there a way to auto get (Sara Smile is * Years of Age), Without a cPanel Error message?

2

Answers


  1. <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="utf-8"> 
    </head>
    <body>
      <?php
        function AutoAge($birthday)
        {
          $today = new DateTime(date('d-m-Y'));
          $bday = new DateTime($birthday); // Persons Date of Birth     
          $diff = $bday->diff($today);
          return $diff->format('%y');
        }
      ?>
      <p>some content here</p>
      <p>Alice has an age of <?php echo AutoAge("11-4-2000"); ?></p>
      <p>other content here</p>
      <p>Bob has an age of <?php echo AutoAge("23-9-2004"); ?></p>
    </body>
    </html>
    

    UPDATE

    Here is an Easier JavaScript Code:

    function AutoAge(birthYear, birthMonth, birthDay)
    {
      var birthdate = new Date(birthYear, birthMonth - 1, birthDay);
      var today = new Date();
      return Math.floor((today.getTime() - birthdate.getTime()) 
        / 1000 / 60 / 60 / 24 / 365);
    }
    
    function showBirthday()
    {
      var i, elem, items = document.getElementsByClassName('birthday');
      for(i=0; i<items.length; i++)
      {
        elem = items[i];
        elem.innerHTML = AutoAge(elem.dataset.year || 2000, elem.dataset.month || 0, elem.dataset.day || 1) + ' years';
      }
    }
        <!DOCTYPE html>
        <html lang="en">
        <head>
          <meta charset="utf-8"> 
        </head>
        <body onload="showBirthday()">
          <p>some content here</p>
          <p>Alice has an age of <span class="birthday" data-year="2000" data-month="4" data-day="11"></span></p>
          <p>other content here</p>
          <p>Bob has an age of <span class="birthday" data-year="2004" data-month="9" data-day="23"></span></p>
        </body>
        </html>
    Login or Signup to reply.
  2. I always use this code to calculate in a elegant way the user age and works fine…

    <!DOCTYPE html>
    
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>User Age Information</title>
    </head>
    
    <body>
    <?php
      function Calc_Age($myday) {
        date_default_timezone_set('Europe/Rome');
        $birth = new DateTime($myday);
        $birth->format('Y-m-d H:i:s');
        $today = new DateTime('NOW');
        $today->format('Y-m-d H:i:s');
        $diffs = $today->diff($birth);
        $myage = $diffs->y . ($diffs->y == 1 ? ' year, ' : ' years, ');
        $myage .= $diffs->m . ($diffs->m == 1 ? ' month, ' : ' months, ');
        $myage .= $diffs->d . ($diffs->d == 1 ? ' day, ' : ' days, ');
        $myage .= $diffs->h . ($diffs->h == 1 ? ' hour and ' : ' hours and ');
        $myage .= $diffs->i . ($diffs->i == 1 ? ' minute' : ' minutes');
        return $myage;
      }
    ?>
    
    <h1>Actual age of Angela</h1>
    
    <p>Angela is <?php echo Calc_Age("1967-01-23 05:00:00"); ?> old</p>
    
    <h1>Actual age of Jhon</h1>
    
    <p>Jhon is <?php echo Calc_Age("1977-04-14 09:10:00"); ?> old</p>
    
    </body>
    
    </html>
    

    Output:

    Age Calc


    Answer with Only Years

    <!DOCTYPE html>
    
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>User Age Information</title>
    </head>
    
    <body>
    <?php
      function Calc_Age($myday) {
        date_default_timezone_set('Europe/Rome');
        $birth = new DateTime($myday);
        $birth->format('Y-m-d');
        $today = new DateTime('NOW');
        $today->format('Y-m-d');
        $diffs = $today->diff($birth);
        $myage = $diffs->y . ($diffs->y == 1 ? ' year' : ' years');
        return $myage;
      }
    ?>
    
    <h1>Actual age of Angela</h1>
    
    <p>Angela is <?php echo Calc_Age("1967-01-23"); ?> old</p>
    
    <h1>Actual age of Jhon</h1>
    
    <p>Jhon is <?php echo Calc_Age("1977-04-14"); ?> old</p>
    
    </body>
    
    </html>
    

    Age Years Only


    Final Notes

    with m-d-Y the php engine cannot calculate the diff dates, to use properly this php class you have to use the standard connotation Y-m-d. This cannot create problem since you know now that you have to call the function with this format

    Calc_Age("1967-01-23"); // Y-m-d (Year-month-day)
    

    Hope this help.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search