我试图在php中计算年龄,但只是想增加年龄,如果出生时间也达到,而不仅仅是生日日期。以下是我目前得到的结果:
$today = date("Y-m-dTH:i:sO");
$bday = "1987-01-01T15:30:00+0200";
$diff = abs(strtotime($bday) - strtotime($today));
$age = floor($diff / (365*60*60*24));
如果到达日期,它会工作,但不具有时间敏感性。谢谢你的帮助!
# object oriented
$from = new DateTime('1987-01-01 T15:30:00+0200');
$to = new DateTime('today');
echo 'Years '.$from->diff($to)->y; echo '<br/>';
echo 'Month '.$from->diff($to)->m; echo '<br/>';
echo 'Days '.$from->diff($to)->d; echo '<br/>';
echo 'Hours '.$from->diff($to)->h; echo '<br/>';
# procedural
echo date_diff(date_create('1987-01-01 T15:30:00+0200'), date_create('today'))->y;
<?php
//Convert to date
$datestr="2015-06-11 19:10:18";//Your date
$date=strtotime($datestr);//Converted to a PHP date (a second count)
//Calculate difference
$diff=$date-time();//time returns current time in seconds
$days=floor($diff/(60*60*24));//seconds/minute*minutes/hour*hours/day)
$hours=round(($diff-$days*60*60*24)/(60*60));
//Report
echo "$days days $hours hours remain<br />";
?>