PHP Date : get date different in years

断了今生、忘了曾经 提交于 2019-12-25 03:53:08

问题


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:


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


来源:https://stackoverflow.com/questions/25560987/php-date-get-date-different-in-years

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!