Event in date and time expiration - PHP

3

My question is this: I created a function in PHP to check the current date and check the deadline for the end of a contest. But the end of this contest will have a specific time, and I do not know how to limit the date along with the time.

The function looks like this:

<?php 
    function isDateExpired($dateStart, $days){

        $timestampNow     = strtotime('now');
        $timestampExpired = strtotime("+{$days} day", strtotime($dateStart));

        return$timestampExpired > $timestampNow) ? true :  false;
    }

    if(isDateExpired('2014-06-09', 1)){
            $teste = 'teste testado e testando';
    }else{
            echo '<script>alert("Concurso encerrado! Aguarde o resultado em breve!");</script>';
            echo '<style>#btlike{display:none;}#btshare{display:none;</style>';
    }

?>

How can I check the time of the next day? For example, the contest will close at 3pm on 06/10/2014.

    
asked by anonymous 09.06.2014 / 17:33

1 answer

4

To make a difference, use the class DateTime > (PHP 5.2.2)

<?php
    date_default_timezone_set('America/Sao_Paulo');

    function IsExpireData($data){
        $data = explode(" ", $data);
        list($d, $m, $y) = explode('/', $data[0]);
        list($g, $i)     = explode(':', $data[1]);
        $dat0 = new DateTime(date("Y-m-d G:i:s", mktime($g, $i, 0, $m, $d, $y)));
        $dat1 = new DateTime(date("Y-m-d G:i:s"));

        $ret = '';
        if ($dat1 == $dat0) {
            $ret = 0;
        } else {
            if ($dat1 < $dat0){
                $ret = 1;
            } else {
                if ($dat1 > $dat0){
                    $ret = -1;
                }
            }
        }
        return $ret;
    }

    $d = IsExpireData("09/06/2014 14:00");
    if ($d == 0 || $d  == -1){
        echo 'Expirado';
    } else {
        echo 'Ainda não expirou';
    }

Example: Ideone

Another option with mktime >

<?php
    function IsExpireData2($data){
        $data = explode(" ", $data);
        list($d1, $m1, $y1) = explode('/', $data[0]);
        list($g1, $i1)      = explode(':', $data[1]);

        $now = date('Y-m-d G:i');
        $now = explode(" ", $now);
        list($y2, $m2, $d2) = explode('-', $now[0]);
        list($g2, $i2)      = explode(':', $now[1]);

        $dat0 = mktime($g1, $i1, 0, $m1, $d1, $y1) / (60 * 60 * 24);
        $dat1 = mktime($g2, $i2, 0, $m2, $d2, $y2) / (60 * 60 * 24);

        $ret = '';
        if ($dat1 == $dat0) {
            $ret = 0;
        } else {
            if ($dat1 < $dat0){
                $ret = 1;
            } else {
                if ($dat1 > $dat0){
                    $ret = -1;
                }
            }
        }
        return $ret;
    }

Example: Ideone

Reference:

09.06.2014 / 18:28