Check interval between datetimes in PHP

后端 未结 3 449
情歌与酒
情歌与酒 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.

提交回复
热议问题