PHP การคำนวณอายุ ความต่างของอายุ php calculate age

PHP โปรแกรมการคำนวณอายุ




ภาษา PHP สามารถทำการหาความต่างของวันเกิด กับ วันที่ปัจจุบันว่ามีระยะเวลาต่างกันเท่าไหร ตอนนี้เราอายุกี่ปีกี่เดือนแล้ว สามารถนำไปประยุกต์ใช้ว่าอีกกีเดือนจะครบกี่ปี ถ้ายังไม่ถึง 18 ปีต้องรออีกกี่เดือนถึงจะอายุ 18

Cal-Age.php


<?php

$birthdate = '19-09-1997';
echo 'Born on : '.$birthdate;

function CalAge($birthdate){
  $today = date('d-m-Y');
    list($bday,$bmonth,$byear) = explode('-',$birthdate);
    list($tday,$tmonth,$tyear) = explode('-',$today);

    if($byear < 1970){
      $yearad = 1970 - $byear;
      $byear = 1970;
    }else{
      $yearad = 0;
    }

    $mbirth = mktime(0,0,0, $bmonth,$bday,$byear);
    $mtoday = mktime(0,0,0, $tmonth,$tday,$tyear);

    $mage = ($mtoday - $mbirth);
    $wyear = (date('Y', $mage)-1970+$yearad);
    $wmonth = (date('m', $mage)-1);
    $wday = (date('d', $mage)-1);

    $ystr = ($wyear > 1 ? " Years" : " Year");
    $mstr = ($wmonth > 1 ? " Months" : " Month");
    $dstr = ($wday > 1 ? " Days" : " Days");

    if($wyear > 0 && $wmonth > 0 && $wday > 0) {
      $agestr = $wyear.$ystr." ".$wmonth.$mstr." ".$wday.$dstr;
     }else if($wyear == 0 && $wmonth == 0 && $wday > 0) {
       $agestr = $wday.$dstr;
     }else if($wyear > 0 && $wmonth > 0 && $wday == 0) {
       $agestr = $wyear.$ystr." ".$wmonth.$mstr;
     }else if($wyear == 0 && $wmonth > 0 && $wday > 0) {
       $agestr = $wmonth.$mstr." ".$wday.$dstr;
     }else if($wyear > 0 && $wmonth == 0 && $wday > 0) {
       $agestr = $wyear.$ystr." ".$wday.$dstr;
     }else if($wyear == 0 && $wmonth > 0 && $wday == 0) {
       $agestr = $wmonth.$mstr;
     }else {
       $agestr ="";
     }

      return $agestr;
    }

echo '<br>Today your age is : '.Calage($birthdate);

?>


ทดสอบรัน http://localhost/projects/cal-age.php


Download Source Code : https://drive.google.com/file/d/0B_mjMFSVx1VndjN1cnFJd0V1Q28/view?usp=sharing

ความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

ใช้ Word ให้เรียกข้อมูลจาก Excel ในแต่ละแถวมาแสดง import data from excel to show in word

การ Comment ในภาษา PHP

แท็ก PHP การเปิดปิดแท็ก