Check if date and time/minute has already expired

Asked

Viewed 88 times

0

I’m having a problem, when I upload my site to PHP 5.6 the code I use to check if such a date and such an hour/minute expired is simply not respecting the time, if it is the date already says that expired without checking the hour/minute. Does anyone have any solution?

$sql = "SELECT * FROM noticias WHERE programada = 'sim'";
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res)>0){
$row = mysql_fetch_array($res);

function NoticiasProgramadas($data){
    $dt_expira = str_replace("/", "-", $data);
    $dateInicio = date('Y/m/d G:i:s', strtotime($dt_expira));
    $timestamp_dt_expira = strtotime($dt_expira);
    $dt_atual = date("Y/m/d G:i:s");
    $timestamp_dt_atual = strtotime($dt_atual);

        $ret = '';
        if ($timestamp_dt_atual >= $timestamp_dt_expira) {
            $ret = "exp";
        }
    return $ret;
}
$d = NoticiasProgramadas("$row[postdate] $row[posthora]");
if ($d == "exp"){

//executa um código mysql

}
  • Echo in the variables $timestamp_dt_atual and $timestamp_dt_expira to see how they are before the if;

  • 11-04-2018, 18:19 2018/04/11 23:19:01

  • No, the same timestamp, before the if ($timestamp_dt_atual >= $timestamp_dt_expira)

  • Sorry, they’re like 1523440560 1523483518

  • Can you simplify our life by editing your question with the data you are using to test? Type what is $data, so we can test your code and help

  • The $date has nothing

  • Forgiveness, that: $row[postdate] $row[posthora]

  • It comes from the first code on top of the $sql = "SELECT * FROM WHERE newsprogrammed = 'yes'";

Show 4 more comments

1 answer

1


Friend,

Check the Timezone of your online server, in some cases before doing anything in PHP, you should set your Timezone so that from now on your PHP will only work with exact date and time, for example:

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

// daqui em diante tu bota o seu código...
  • It is already set in the same file Brazil/East

Browser other questions tagged

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