我希望使用时间戳和当前日期提醒一天。想知道我是否可以使用这些进行简单的减法。
如何计算currentdate -timestamp?
答案 0 :(得分:3)
PHP的时间戳与Unix时间戳相同 - 自1970年1月1日起的秒数。所以是的,一个简单的减法会给你一个秒的时差,你可以通过潜水86,400(一天中的秒数)转换为天数:
$days = (time() - $oldtimestamp) / 86400;
答案 1 :(得分:2)
还有使用DateTime
和DateInterval
类的首选选项。
$now = new DateTime;
$then = new DateTime;
$then->setTimestamp($timestamp);
$diff = $now->diff($then);
echo $diff->days;
以上内容还将提供您感兴趣的年数,月数,天数等(以及显示的总天数)。
答案 2 :(得分:0)
试试这个:
// Will return the number of days between the two dates passed in
function count_days( $a, $b )
{
// First we need to break these dates into their constituent parts:
$gd_a = getdate( $a );
$gd_b = getdate( $b );
// Now recreate these timestamps, based upon noon on each day
// The specific time doesn't matter but it must be the same each day
$a_new = mktime( 12, 0, 0, $gd_a['mon'], $gd_a['mday'], $gd_a['year'] );
$b_new = mktime( 12, 0, 0, $gd_b['mon'], $gd_b['mday'], $gd_b['year'] );
// Subtract these two numbers and divide by the number of seconds in a
// day. Round the result since crossing over a daylight savings time
// barrier will cause this time to be off by an hour or two.
return round( abs( $a_new - $b_new ) / 86400 );
}