简体   繁体   中英

Getting year month day and age between Date and date now using PHP

I have the code as below:

<tr>
  <td>Date :</td>
  <td><input type="text" name="txtdembauche" value="<?php echo $value['dateEmbouche'] ?>"class="validate[required] text-input datepicker TextInput"  id="txtdembauche"/></td>
  <td></td>
</tr>
<?php                           

  @$date1 = $_POST['txtdembauche'];
  $date2 = date("Y-m-d");
  $diff = abs(strtotime($date2) - strtotime($date1));
  $years = floor($diff / (365*60*60*24));
  $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
  $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));     

?>
<tr>
  <td>Ancienneté : <b id="anciente" style="color:#FF0000;"><?php printf("%d years, %d months, %d days\n <br />", $years, $months, $days); ?></b></td>
  <td></td>
  <td></td>
</tr>
<input type="text" name="dateResult" id="dateResult" value="<?php // I do not know how to calculate this ?>">

Problem:

<tr>
  <td>Ancienneté : <b id="anciente" style="color:#FF0000;"><?php printf("%d years, %d months, %d days\n <br />", $years, $months, $days); ?></b></td>
  <td></td>
  <td></td>
</tr> 

It not works well,it display the wrong date.

<input type="text" name="dateResult" id="dateResult" value="<?php // I do not know how to calculate this ?>">

I want to get the age of person between date and date now ex: 12years , but I do not know how to do this.

<?php
$birthday = "1987-01-01";
$timestamp = strtotime($birthday);
$age = (date("md") >= date("md", $timestamp)) ? (date("Y") - date("Y", $timestamp)) : (date("Y") - date("Y", $timestamp) - 1);
echo $age."\n";

Hope this can help you!

As for comments this is something about your coding technique or the concept. I have created a test code just follow it and identify where you went wrong

<?php                           
if(isset($_POST['submit'])){
  @$date1 = $_POST['txtdembauche'];// if format mm/dd/yyyy
  $date2 = date("m/d/Y");//change m,d,y according to input data format
  $diff = abs(strtotime($date2) - strtotime($date1));
  $years = floor($diff / (365*60*60*24));
  $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
  $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));     
}
?>
<form action="" method="post">
<table>
<tr>
  <td>Date :</td>
  <td><input type="text" name="txtdembauche" value="<?php echo $value['dateEmbouche'] ?>"class="validate[required] text-input datepicker TextInput"  id="txtdembauche"/></td>
  <td><input type="submit" name="submit" value="Get Age"></td>
</tr>
<tr>
  <td>Ancienneté : </td>
   <td><b id="anciente" style="color:#FF0000;"><?php printf("%d years, %d months, %d days\n <br />", $years, $months, $days); ?></b></td>
  <td></td>
</tr>
<tr>
  <td>Age</td>
  <td><input type="text" name="dateResult" id="dateResult" value="<?php echo $years; ?>"></td>
  <td></td>
</tr>
</table>
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM