计算时间戳之间的差异

时间:2012-07-04 08:42:54

标签: php html database codeigniter timestamp

我创建了注册页面,当用户单击“提交”按钮时,会向其电子邮件发送激活链接,因此时间戳存储在数据库中。如果用户单击该激活链接,我必须检查在24小时之前或之后是否单击了该链接。 我的代码: -

function confirmEmail($activation_code){
        $this->load->database();
        $this->load->helper('date');
        echo "activation link will be checked and accordingly flag will be set.";
        $activation_sent_timestamp=$this->db->query("SELECT activation_timestamp FROM tbl_user_registration WHERE email_verification_code='$activation_code'");
        foreach($activation_sent_timestamp->result() as $res){
            $activation_time_from_db=$res->activation_timestamp;
        }
        echo $activation_time_from_db."\n\r";
        $now = time();
        $human = unix_to_human($now);
        echo $human;
        $difference = ($human-$activation_time_from_db);
        if($difference < 24) {
               echo "correct"
        }
        else echo "Link expired";
    }

我正在使用codeigniter。我怎么能这样做,这段代码没有显示任何错误,但我不知道这是24小时计算的正确方法,我正在检查但没有得到任何东西。请检查代码。

已解决........:)

6 个答案:

答案 0 :(得分:1)

unix_to_human()只返回一个人类可读的时间戳形式

最简单的方法是,找出两个时间戳之间的差异,转换为小时并检查它是否小于24小时

答案 1 :(得分:0)

您可能想要使用date_diff

见这里:DATE_DIFF()

答案 2 :(得分:0)

您可以使用差异计算差异,也可以更改时间格式

$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');

答案 3 :(得分:0)

将两个时间戳转换为php date('Y-m-d')格式,然后尝试 date_diff

答案 4 :(得分:0)

您可以在查询中使用mysql timediff函数 看看你的情况你可以这样做,因为我在这篇文章中已经回答了

Finding free blocks of time in mysql and php?

答案 5 :(得分:0)

$time = time();

$seconds = 86400 // seconds of 24 Hours

SELECT * FROM tbl_user_registration WHERE ( $time - UNIX_TIMESTAMP( activation_timestamp ) >= $seconds )
相关问题