php get years between two dates
取2日期間共幾年
/* get years between two dates https://vector.cool date $date ex:2006/3/8 date $date2 ex:2017/5/14 return int */ function get_years_between_two_dates($date,$date2=NULL){ $time = (empty($date2))?time():strtotime($date2); $time = $time - strtotime($date); return intval($time / (365*24*60*60)); }
用法:
這邊有個要注意的地方,使用這方法須先設定時區
date_default_timezone_set()
date_default_timezone_set("Asia/Taipei"); echo get_years_between_two_dates('2007/7/28','2017/12/23');
[……]