我需要找到一种方法,以秒为单位来生成时间戳,该时间戳表示1970年之前的日期到当前时间之间的间隔。
X人生日1969-01-02
2013-08-23
我试过了
<?php
$then = new DateTime("1969-01-02");
$age = $then->getTimestamp();
?>在1970年之前给了我一个负时间戳。
我也做了一个日期差异
<?php
$then = new DateTime("1969-01-02");
$now = new DateTime("now");
$age = $then->diff($now);
?>但我不知道如何将日期差异转换为我可以使用的秒时间戳。有什么帮助吗?
我问的原因是,相对于现在,在1970年以后的某个日期执行时间戳时,它工作得很好,并给出了预期的输出。但是,如果一个人的生日是1970年之前,它将只是计算他们出生日期到1970年的时间戳,这是完全不准确的。因此,我需要找到一种方法,以秒为单位,为1970年前出生日期的人获得他们年龄的时间戳。
发布于 2013-08-23 11:03:46
这就是人们所期望的行为。
UNIX timestamp是自Jan 1 1970 (不考虑闰秒)后的秒数()。对于在此之前的日期,它将return一个负数。
是的,,如果您想要计算years, months and days,那么试试这个,
<?php
$then = new DateTime("1969-01-02");
$now = new DateTime('now');
$age = $then->diff($now);
echo $age->format('%Y years, %m months,%d days');
?>更新,
<?php
$then = strtotime("1969-01-02");
$now = strtotime('now');
$age=$now-$then;
// Above is your final time stamp, created by the difference of two dates
$diff = abs($age);
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
printf("%d years, %d months, %d days\n", $years, $months, $days);
// Outputs-> 44 years, 8 months, 4 days
?>https://stackoverflow.com/questions/18400949
复制相似问题