这是格式化时间戳的方法:
echo date('d-m-Y', strtotime($end_date)) // for DD-MM-YYYY format
您是否要计算 2 个日期之间的差异(以天为单位)?
编辑:用于查找“XXyear YYmonth ZZday”中日期之间差异的代码。该代码假定开始日期和结束日期采用 YYYY-MM-DD 格式。如果您不是这种情况,请将它们更改为 YYYY-MM-DD 格式,或者相应地将参数更改为 mktime()。
$endDate = '2011-03-01';
$startDate = '2011-02-02';
$daysPerYear = 365;
$daysPerMonth = 30.4;
$diffDay = $diffMonth = $diffYear = 0;
$endDateTs = mktime(0, 0, 0, substr($endDate, 5, 2), substr($endDate, 8, 2), substr($endDate, 0, 4));
$startDateTs = mktime(0, 0, 0, substr($startDate, 5, 2), substr($startDate, 8, 2), substr($startDate, 0, 4));
$diffDay = ($endDateTs - $startDateTs) / 60 / 60/ 24; // difference between 2 dates in number of days
$diffYear = floor($diffDay / $daysPerYear); // difference in years
$diffDay = $diffDay % $daysPerYear; // balance days
$diffMonth = floor($diffDay / $daysPerMonth); // difference in months
$diffDay = ceil($diffDay % $daysPerMonth); // balance days
echo ($diffYear ? $diffYear . 'year ' : '') . ($diffMonth ? $diffMonth . 'month ' : '') . ($diffDay ? $diffDay . 'day' : '');
注意:我没有测试所有可能的日期组合的代码,包括闰年等。请随时根据需要进行调整。
希望这会有所帮助。