简体   繁体   中英

Codeigniter : Difference Between two time stamps

I am working in codeigniter. I need to take the difference between two time stamps and use this for link expiry in forgot password service.

So far I have tried.

$date= date('Ymd H:i'); this will produce something like 2017-03-20 12:02 .

Then again in the near future I want to use the same function to get the currunt time. Eventually measure the difference between these two times in minutes.

The best way to achieve this is to make an extra column like created_at that contains the datetime of link creation in it. When user click on the link get the current date and time and calculate if the difference is acceptable or not as per your policy.

If you are not using database, then add an extra parameter in your link like created_at or valid_upto with encrypted timestamp in it and use it to calculate the time difference and validate it.

You can save your time in unix timestamp using time() function and use simple subtraction for calculate difference b/w two timestamp. it will return number of second b/w two time then convert second to minute dividing by 60.

OR

you can use strtotime() function to convert date into time stamp.

    $past_time = strtotime('2017-03-20 12:02');
    $current_time = time();
    $difference = $current_time - $past_time;
    $difference_minute =  $difference/60;
    echo 'Difference in minute between two different Time : '.intval($difference_minute);exit;

You can try with this code. This code for give a difference in minutes between two different Time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM