0

I have created registration page and when user click submit button, an activation link is sent to his email and accordingly timestamp is stored in the database. If user click that activation link, I have to check whether that link is clicked before or after 24 hours . my code :-

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";
    }

I am using codeigniter. How can I do this, this code isnot showing any erros but I dont know is this the right way to calculate 24 hours, I am checking but didnt get anything.please check the code.

SOLVED........ :)

avinashse
  • 1,440
  • 4
  • 30
  • 55
  • SUGGESTION:I always try and keep php dates and mysql dates separate, as there is sometimes a mismatch. If you inserted data with NOW() then use DATE_ADD and DATE_SUB with INTERVAL 1 DAY. If field is datetime use UNIX_TIMESTAMP() to convert it. If you inserted with 'time()' then strtotime('+1 day') can be used. – Waygood Jul 04 '12 at 08:52

6 Answers6

1

unix_to_human() just returns a human readable form of timestamp

The simplest method is, find the difference between both time stamps, convert to hrs and check if it is less than 24hrs

Quicksilver
  • 2,546
  • 3
  • 23
  • 37
  • You are saying to do this `$sent_time=human_to_unix($activation_time_from_db); $now = time(); echo ($now-$sent_time);` How to cnonvert this time into hours..? – avinashse Jul 04 '12 at 09:05
0

You may want to use date_diff

See here : DATE_DIFF()

BMN
  • 8,253
  • 14
  • 48
  • 80
0

You can use diff for calculation difference and also you can change time format

$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
Ravi Kant Mishra
  • 778
  • 1
  • 7
  • 13
0

Convert both timestamps to php date('Y-m-d') formats then try date_diff

Krishna
  • 353
  • 2
  • 15
0

You can use mysql timediff function in the query.
Looking at your situation you can do this as i have answered in this post

Finding free blocks of time in mysql and php?

Community
  • 1
  • 1
Muhammad Raheel
  • 19,823
  • 7
  • 67
  • 103
0
$time = time();

$seconds = 86400 // seconds of 24 Hours

SELECT * FROM tbl_user_registration WHERE ( $time - UNIX_TIMESTAMP( activation_timestamp ) >= $seconds )
Alireza
  • 1,428
  • 4
  • 21
  • 33
  • Showqing `Undefined property: stdClass::$activation_timestamp` error. :( – avinashse Jul 04 '12 at 09:16
  • activation_timestamp is the name of your table's column if( count($activation_sent_timestamp->result()) ) { //You have expired links } – Alireza Jul 04 '12 at 10:02