Check interval between datetimes in PHP

后端 未结 3 450
情歌与酒
情歌与酒 2021-01-28 13:33

I have a field in database that has type of datetime in which I add time when user visit a page. When user again comes I want to check the interval between his first visit and c

相关标签:
3条回答
  • 2021-01-28 14:04

    What you can so is, retrieve the the datetime, store it in a variable. Create a var with time().

    You can then convert the db datetime to a timestring using strtotime()

    Subtract the datetime timestring from the new time. That should give you a difference in seconds. You can then manipulate your values and do the relevant checks.

    $db = datetime_from_database;

    $now = time();

    $last = strtotime($db);

    $diff = $now - $last; //this is in seconds

    You can do something like

    $minutes = $diff / 60;

    If ($minutes > 60) echo 'more than 1 hour; 60 minutes';

    Just work in it.

    You can then use the date functions to format the new datetime using the $now and update the database.

    0 讨论(0)
  • 2021-01-28 14:17

    You could try

    SELECT COUNT(@UserID) FROM table WHERE LastVisit > (DateADD(now(),interval -1 Hour))
    

    you can then check the count

    Edit: added FROM clause

    0 讨论(0)
  • 2021-01-28 14:23

    If you have PHP >= 5.3 you can use DateTime objects and functions:

    $visit = DateTime::createFromFormat('Y-m-d H:i:s', '2011-03-04 00:25:01');
    $now = new DateTime("now");
    
    $diff = $now->diff($visit);
    
    0 讨论(0)
提交回复
热议问题