Date and time expiration event - PHP

Asked

Viewed 1,253 times

3

My question is the following: I created a function in PHP to check the current date and check the deadline for the end of a contest. Except that the end of this contest will have a specific time, and I don’t know how to limit the date along with the time.

The function is 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 15hrs of the day 10/06/2014.

  • Since it will be by hour, you can take the difference between dates by HOURS and put in $timestampExpired = strtotime("+{$hours} hour", strtotime($dateStart));

  • Thanks Victor, thanks a lot!

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:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.