PHP Date : get date different in years

2019-08-20 05:37发布

this is my code:

date_default_timezone_set('Asia/Kuala_Lumpur');
$date_join = $row['date_joined']; =produce 2012-09-03
$today = date("Y-m-d");   = produce 2014-08-29

$objPHPExcel->setActiveSheetIndex(0)->setCellValue('H'.$a, **$xxx**);

how can i get date durations ($today - $date_join) in years like :

Date of services : 1.5 years

1条回答
成全新的幸福
2楼-- · 2019-08-20 06:28

You can use PHP strtotime() function. Try like this..

$join_date = '2012-09-03'; //join date
$today = date("Y-m-d"); //current date

$date_join  = strtotime($join_date); // join date to seconds
$today = strtotime($today); //current date to seconds

$differenceInSeconds = $today - $date_join; // Time difference in seconds
echo number_format($differenceInSeconds / (365 * 24 * 60 * 60), 2) . ' Year(s)';
  • Get time difference in seconds from two dates
  • Divide the difference by one year equivalent seconds
查看更多
登录 后发表回答