0

我有如下代码:

<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 ?>">

问题:

<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 ?>">

我现在想得到日期和日期之间的人的年龄 ex: 12years,但我不知道该怎么做。

4

2 回答 2

1
<?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";

希望这可以帮到你!

于 2012-10-26T03:25:31.143 回答
0

至于评论,这与您的编码技术或概念有关。我已经创建了一个测试代码,只需按照它并确定你哪里出错了

<?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>
于 2012-10-26T03:14:50.307 回答