Sunday, 15 July 2012

php - Working with DateTime -



php - Working with DateTime -

i working on datetime. need age between 2 date. date1 retrieve database while date2 today.below code:

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

then code 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>

might suggest using first-class carbon package? provides great tools messing datetimes, including finding difference in months, years, etc.

use carbon\carbon; $now = carbon::now(); $registrationdate = carbon::parse($regdate); // user on registration anniversary? $anniversary = $now->isbirthday($registrationdate); // years since registering (rounded down, 11 months , 30 days = 0 years) $years = $now->diffinyears($registrationdate);

php

No comments:

Post a Comment