PHP-以分钟为单位显示链接过期时间


PHP - Show link expiration time in minutes

我想用php:设置一个链接过期日期

我希望当用户在我的网站上创建一个新的短链接时,它应该在创建的第五天自动删除。

我对以下代码完全感到困惑。我想把这个代码放在用户的通知页面上,这样它就可以通知他们还有多少分钟的时间可以让链接过期。

<?php 
$created=time();
$expire=$created + 5;
$total_minutes=$expire / 5 / 60;
echo "Link expires in $total_minutes minutes";?>

它输出一个意外的长数字。

如何实现此代码,使其可以输出7200分钟或剩余分钟?

time()返回UNIX时间戳。

如果您想要人类可读的输出,请查看PHP中的DateTime类:http://php.net/manual/en/class.datetime.php

示例:

<?php
$created = new DateTime('now');
$expiration_time = new DateTime('now +5minutes');
$compare = $created->diff($expiration_time);
$expires = $compare->format('%i');
echo "Your link will expire in: " . $expires . " minutes";
?>
php函数time()以秒为单位返回(自Unix Epoch以来)。

你加"5"只需要5秒。

对于五天,您需要添加5 * 24 * 60 *60的总和,即五天的秒数。

代码中:

$created = time();
$expires = $created + (5 * 24 * 60 * 60);
if ($expires < time()) {
    echo 'File expired';
} else {
    echo 'File expires in: ' . round(((time() + 1) - $created) / 60) . ' minutes';
}

请参阅PHP:time()

<?php
$created = strtotime('June 21st 20:00 2015'); // time when link is created
$expire = $created + 432000; // 432000 = 5 days in seconds
$seconds_until_expiration = $expire - time();
$minutes_until_expiration = round($seconds_until_expiration / 60); // convert to minutes
echo "Link expires in $minutes_until_expiration minutes";
?>

请注意,$created不应该在脚本运行时生成,而是保存在某个地方,否则此脚本将始终报告链接在5天后过期。