-1

I am working on datetime. I need the age between two date. Date1 retrieve from database while date2 is today.Below is the code:

while($row= mysqli_fetch_array($query)){
    $surname=$row['surname'];
    $firstname= $row['firstname'];
    $othername=$row['othername'];
    $sex= $row['sex'];
    $regdate= $row['reg_date'];
}

then this is code for table display:

<tr>
    <td>Years in Service</td>
    <td><?php 
    $date1 = new DateTime("Y-m-d");
    $date2 = new DateTime("$regdate");
    $interval = $date1->diff($date2);
    echo  $interval->y . " years, " . $interval->m." months, ".$interval->d." days ";   
    ?></td>
</tr>
Naruto
  • 1,210
  • 3
  • 25
  • 28
David Mukoro
  • 467
  • 1
  • 8
  • 25

2 Answers2

0

Might I suggest using the excellent Carbon package? It provides some great tools for messing with DateTimes, including finding the difference in months, years, etc.

use Carbon\Carbon;

$now              = Carbon::now();
$registrationDate = Carbon::parse($regdate);

// is the user on their registration anniversary?
$anniversary = $now->isBirthday($registrationDate);

// years since registering (rounded down, so 11 months and 30 days = 0 years)
$years = $now->diffInYears($registrationDate);
alexrussell
  • 13,856
  • 5
  • 38
  • 49
  • Pls Alexrussell, when attempting to use carbon, below is the error: Fatal error: Class 'Carbon\Carbon' not found in C:\wamp\www\medics\staff_upgrade.php on line 101. Also, I want to be able to allow a user to supply his date of birth in a field and the page calculate his age and display it in the next field ,all in the same form. – David Mukoro Oct 30 '14 at 08:01
  • Carbon is a third-party library and so you must actually import its code into your project. Most people these days will use a piece of software called [Composer](https://getcomposer.org/) for this - it'll handle and dependencies and set up a class autoloader for you. However, if you don't want to use Composer, you can just get the Carbon code and [load in the class manually](https://github.com/briannesbitt/Carbon#install-nocomposer). – alexrussell Oct 30 '14 at 08:44
  • As for your second question, you should really post that as a new Stack Overflow question. However, if you need the calculation to happen in real-time you either need to use JavaScript to do this or use AJAX to POST to PHP and get the result. FWIW Carbon will easily allow you to do this (see my code above, the last line), but yeah if you need it to all work on the page, you should look into a JS solution to your problems maybe? You will find [moment.js](http://momentjs.com/docs/#/displaying/difference/) works quite a bit like Carbon and allows this. – alexrussell Oct 30 '14 at 08:47
-1

Try this:

$date1 = time();
$date2 = strtotime($regdate);
$diff = abs($date2-$date1); // two timestamps
echo 'its '.$diff.' seconds!';

or:

NICE SOLUTION

Community
  • 1
  • 1
brandelizer
  • 342
  • 3
  • 17
  • Thanks Brandelizer. The result of the computation is a long value in second. What is the conversion to years?Like 1672508 – David Mukoro Oct 30 '14 at 07:36
  • First do $diff/(60*60*24*365) if there is a rest you make it for months and so on... – brandelizer Oct 30 '14 at 08:03
  • http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php – brandelizer Oct 30 '14 at 08:06
  • Brandelizer, ihave gotten the solution to conversion by multiplying the result with number_format(($ageCal*3.1689e-8),0).It works greatly. However, i another issue with another form. A form with many text field. Eg field1: surname,field2:dateofbirth,field3:age. Now i want the user to supply the first two, why the page automatically echo the third field on spot.Any help??? Appreciate assitance. – David Mukoro Oct 30 '14 at 08:09
  • why you have a age field? You know the age allready!! And I can't answer your question with this low info.... – brandelizer Oct 30 '14 at 08:23
  • Pls, understand, I said the first question of age difference has been solved. Now I have another page in which i want to implement this code as well, but with different orientation.In the first case, the age difference is calculated from date1 fetch from the Database and today(time()) whereas in this second question: the age difference will be calculated from the date of birth supplied in a field by the user and today(time()) without clicking any button. Something like at the focus event of the age, output the diff btw supplied date of birth and today's date. I just need to translate the above – David Mukoro Oct 30 '14 at 08:42
  • Make it with js (jQuery) in the same way as i posted before. – brandelizer Oct 30 '14 at 09:02
  • All you need is jQuery: keyup, or change functions help you – brandelizer Oct 30 '14 at 09:03
  • http://stackoverflow.com/questions/26634266/get-result-of-a-multiplication-of-two-input-tags-into-a-different-one/26634715#26634715 – brandelizer Oct 30 '14 at 09:04