2015-06-21 44 views
-1

我想设置一个链接失效日期用PHP:PHP - 以分钟为单位显示链接过期时间

我想,当用户创建我的网站上一个新的短链接则应该会自动创建之第五天删除。

我完全与下面的代码混淆。我想把这段代码放在用户的通知页面上,这样它可以告诉他们有多少分钟的时间会让链接过期。

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

它输出一个意外的长数。

我该如何实现此代码才能输出7200或剩余的分钟数?

+0

如果你只需要在几分钟的剩余时间量,足以打印“5 * 24 * 60”,其中5架5天。以下链接将为您提供完成任务所需的信息:http://php.net/manual/en/function.time.php – varnie

+0

您在哪里存储链接及其创建和到期时间?我假设在一个数据库中,有一个userid密钥? –

回答

1

time()返回UNIX时间戳。

如果你想人类可读的输出,考虑DateTime类在PHP中: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"; 

?> 
1
<?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"; 
?> 

注意,创建$不应在脚本运行时进行,但保存某处,否则此脚本将始终报告该链接在5天内过期。

+0

Word诞生了。我还没有测试过代码,是的,我忘了分60分钟来换个分钟。我现在编辑。 –

1

php函数time()以秒为单位返回(自Unix时代起)。

添加“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()

相关问题