Comparison of two dates in php

0

I am trying to compare 2 dates in my sentence .. I am more than sure that I must return a record. but I do not know why the condition is not met ... any ideas?

    global $wpdb;

$usuarios = $wpdb->get_results("SELECT m.*, u.user_login
                               FROM {$wpdb->prefix}usermeta as m
                               inner join {$wpdb->prefix}users as u
                               where m.meta_key = 'wp-last-login' and m.user_id = 516 and u.id = 516", OBJECT );
$paginas = $wpdb->get_results("SELECT p.*
                               FROM {$wpdb->prefix}posts as p
                               ", OBJECT );
if ($usuarios) {
    foreach ($usuarios as $u) {
        var_dump($fecha_login = gmdate("Y/m/d H:i:s", $u->meta_value));
        foreach ($paginas as $p) {
            $fecha_login = gmdate("Y/m/d H:i:s", $u->meta_value);
            $fecha_pagina = gmdate("Y/m/d H:i:s", $p->post_modified);
            // echo $fecha_pagina.'=>'.$fecha_login;
            // echo '<br>'.'--------------'.'<br>';
            if($fecha_pagina > $fecha_login) {
                var_dump("entro");
            }
        }
    }
}
    
asked by Hernan Chaparro 13.12.2018 в 13:49
source

1 answer

1

Without going into much detail, I recommend you try this:

global $wpdb;

$usuarios = $wpdb->get_results("SELECT m.*, u.user_login
                           FROM {$wpdb->prefix}usermeta as m
                           inner join {$wpdb->prefix}users as u
                           where m.meta_key = 'wp-last-login' and m.user_id = 516 
                           and u.id = 516", OBJECT );
$paginas = $wpdb->get_results("SELECT p.*
                           FROM {$wpdb->prefix}posts as p
                           ", OBJECT );
if ($usuarios) {
    foreach ($usuarios as $u) {
        foreach ($paginas as $p) {
            var_dump("Estoy comparando esto: ".$u->meta_value." con esto: ".$p->post_modified );
            if( $u->meta_value > $p->post_modified ) {
                var_dump("entro y sigo con la vida");
                $fecha_login = gmdate("Y/m/d H:i:s", $u->meta_value);
                $fecha_pagina = gmdate("Y/m/d H:i:s", $p->post_modified);
            }
        }
    }
}

Basically what I did is compare the data as it comes from the database, without transforming it to gmdate, because remember that those functions change the data type, so that the & lt ;, & gt ;, > = , < = may not apply for that type of data. Once the comparison is made, if everything goes well you can use the gmdate to leave it in a more human format.

Also, I added a var_dump that shows what you are comparing in each iteration.

I hope to contribute

    
answered by 13.12.2018 / 16:45
source