php – WPCode – Modified date comparison with post date not working properly

Question

I am working in WPCode to get the modified/updated timestamp of a post to show alongside the author of the article. So far the code will print “Last Updated On: <Month day, year> by X” correctly on each post. But upon further inspection I noticed that it is not doing the following comparisons between the posted date and the modified date. The script is simply outputting the updated date no matter if I use >= or <=, when I use > or < it will not show a date. It never shows the else case.

Some articles were scheduled so the modified date is before the post date. But it just seems that the comparison is not actually happening or it would pick the post date instead of the modified date. I have done quite a bit of research but was not able to find a solution for what seems to be a bug. Anything will help. Thanks in advanced.

$u_post_date       = get_post_time( 'U' );
$u_modified_date   = get_the_modified_time( 'U' );

add_filter( 'generate_post_date_output', function( $output, $time_string ) {
    
    if ( $u_modified_date >= $u_post_date ) {

        $updated_date = get_the_modified_time( 'F jS, Y' );
        $updated_time = get_the_modified_time( 'h:i a' );
        $time_string = '<time class="entry-date updated-date" datetime="%1$s" itemprop="dateModified">Last Updated On: %2$s</time>';

        $time_string = sprintf( $time_string,
                               esc_attr( $updated_time ),
                               esc_html( $updated_date )
                              );

        return sprintf( '<span class="posted-on">%s</span> ',
                       $time_string
                      );
    }
    else {
        $posted_date = get_the_time( 'F jS, Y' );
        $posted_time = get_the_time( 'h:i a' );
        
        $time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">Published on: %2$s</time>';
        
        $time_string = sprintf( $time_string,
                               esc_attr( $posted_time ),
                               esc_html( $posted_date )
                              );

        return sprintf( '<span class="posted-on">%s</span> ',
                       $time_string
                      );
    }
}, 10, 2 );                                               

0
user3853980 3 weeks 2024-02-12T21:47:18-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse