I need to find a way to find out how to produce a timestamp in seconds that represents the interval between a date that is before 1970 to the current time.
我需要找到一种方法来找出如何以秒为单位生成时间戳,该时间戳表示1970年之前的日期与当前时间之间的时间间隔。
X persons birthday 1969-01-02
X人生日1969-01-02
Current Day 2013-08-23
当前日2013-08-23
I tried
<?php
$then = new DateTime("1969-01-02");
$age = $then->getTimestamp();
?>
Gave me a minus timestamp up until 1970.
给我一个减去时间戳直到1970年。
I have also done a date difference
我也做了一个日期差异
<?php
$then = new DateTime("1969-01-02");
$now = new DateTime("now");
$age = $then->diff($now);
?>
But I don't know how to convert the date difference into a seconds timestamp that I can use. Any help please?
但我不知道如何将日期差异转换为我可以使用的秒时间戳。有什么帮助吗?
The reason I ask is that when performing a timestamp on a date after 1970 relative to now it works fine and gives the expected output. But if someones birthday is before 1970 it will just calculat the timestamp of their date of birth until 1970 which is totally inaccurate. So I need to find a way to get a timestamp of their age in seconds for people with date of births before 1970.
我问的原因是,当相对于现在在1970年之后的日期执行时间戳时,它工作正常并给出预期的输出。但是,如果某人的生日是在1970年之前,它将只计算他们的出生日期到1970年的时间戳,这是完全不准确的。因此,我需要找到一种方法,以便在1970年之前为出生日期的人提供他们年龄的时间戳。
1 个解决方案
#1
0
That's the expected behavior.
这是预期的行为。
UNIX timestamp
is the number of seconds since Jan 1 1970
(not considering leap seconds). For dates before that, it will return
a negative number.
UNIX时间戳是自1970年1月1日以来的秒数(不考虑闰秒)。对于之前的日期,它将返回一个负数。
Yes, if you want to calculate the years, months and days
then try this,
是的,如果你想计算年,月和日,那么试试这个,
<?php
$then = new DateTime("1969-01-02");
$now = new DateTime('now');
$age = $then->diff($now);
echo $age->format('%Y years, %m months,%d days');
?>
Updated,
<?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
?>
#1
0
That's the expected behavior.
这是预期的行为。
UNIX timestamp
is the number of seconds since Jan 1 1970
(not considering leap seconds). For dates before that, it will return
a negative number.
UNIX时间戳是自1970年1月1日以来的秒数(不考虑闰秒)。对于之前的日期,它将返回一个负数。
Yes, if you want to calculate the years, months and days
then try this,
是的,如果你想计算年,月和日,那么试试这个,
<?php
$then = new DateTime("1969-01-02");
$now = new DateTime('now');
$age = $then->diff($now);
echo $age->format('%Y years, %m months,%d days');
?>
Updated,
<?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
?>