Search code examples
phpcookiessession-cookies

How Admin Can Unset All The Cookies Of The Users


I have a website that makes art contest every day and at 9 pm the admin decides someone as the winner he also wants to unset all the cookies of all the users.

My Cookie that I am setting for one day

$cookie_name = "participated";
$cookie_value = "yes";
    
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); 

Is there any way that It can be unset every day at 9 pm or is there any way that the admin can unset all the cookies every day at 9 pm?

Edited : Code tried

<?php
date_default_timezone_set('Asia/Kolkata');

if(date("H") <= 12){
    $date = date("H");
}else{
    
$date = date("H")-12;
}

echo 9-$date;
?>

Solution

  • Using the DateTime builtin class you can simply do

    $cookie_name = "participated";
    $cookie_value = "yes";
    
    // get the timestamp of today @ 9PM to use as the expiry time
    $expire = (new DateTime('today 21:00:00'))->getTimestamp();
    
    // or with a specific timezone
    //$expire = (new DateTime('today 21:00:00', new DateTimeZone('Asia/Kolkata')))->getTimestamp();
    
    setcookie($cookie_name, $cookie_value, $expire, "/");