设置饼干与时间


Set cookie with time

需要检查cookie以查看时间是否小于当前时间减去一小时。但是我当前的模型不起作用。基本上,这将在将饼干全部移除之前,在一定的时间间隔内对饼干进行几次测试。

    <?php
        if (isset($_COOKIE['cookieTest'])) {
            if ($_COOKIE['cookieTest'] < (time() - (60*60))) {
                $content = '<h2 style="color:green;font-weight:bold;">Cookie set a minute again.</h2>';
                unset($_COOKIE['cookieTest']);
            } else {
                $content = '<h3 style="color:red;">Hasn''t been a minute!</h3>';
            }
        } else {
            setcookie('cookieTest',
                        time(), 
                        (time()+3600), 
                        '/', 
                        'bfxsocial.strangled.net'
                    ) or die('<!DOCTYPE html>
                                <html lang="en">
                                    <head>
                                        <title>Test Cookie</title>
                                    </head>
                                    <body>
                                        <h3 style="color:red;">Cookie failed to be set</h3>
                                    </body>
                            </html>');
            $content = '<h3 style="color:orange;">Cookie just set</h3>';
        }
    ?>
    <!DOCTYPE html>
    <html lang="en">
        <head>
            <title>Test Cookie</title>
        </head>
        <body>
            <?php echo $content; ?>
        </body>
    </html>

我不确定最终目标是什么或您要完成什么,但也许这会有所帮助:

<?php
date_default_timezone_set('America/Chicago');
$time = $_COOKIE['cookieTest']; // Timestamp to test
$t = '1 hour'; // Length in the past
if($time < strtotime("- $t"))
{
    //Timestamp is older than $t.
}
else
{
    //Timestamp is NOT older than $t.
}