Support » Developing with WordPress » How do I print “today” and “yesterday”, replacing post date on post entries?

  • On post entries of posts that were posted today and yesterday, I want to display the labels “today” and “yesterday”.

    Ho do I do this with PHP?

    Here is a sample block filter that I can use for this purpose in my own setup :

    function prefix_register_block( $blocks ) {
    	
    	// 'my_block' corresponds to the block slug.
    	$blocks['my_block'] = [
    		'name' => __( 'My Block', 'text-domain' ),
    		'render_callback' => 'prefix_my_block_render',
    	];
    
    	return $blocks;
    	
    }
    
    add_filter( 'wp_grid_builder/blocks', 'prefix_register_block', 10, 1 );
    
    // The render callback function allows to output content in cards.
    function prefix_my_block_render() {
    
    	// Get current post, term, or user object.
    	$post = wpgb_get_post();
    
    	// Output the post title.
    	echo '<h3>' . esc_html( $post->post_title ) . '</h3>';
    
    }

    How do I modify my block filter and add php code so that the respective labels will be displayed?

Viewing 9 replies - 1 through 9 (of 9 total)
  • There are some core functions for human readable time: https://developer.www.4f19.cn/?s=human

    Edit: I forgot to mention that a filter function should not echo anything. It should always return its value, so that there can be more than one filter applied.

    Thanks.

    What should I write instead of this line

    echo '<h3>' . esc_html( $post->post_title ) . '</h3>';

    to display a “today” and “yesterday” label on entries of posts posted today and yesterday?

    I found this, I am not sure how to implement it:
    https://stackoverflow.com/questions/3454258/php-date-yesterday-today

    • This reply was modified 1 month, 2 weeks ago by berry metal.
    • This reply was modified 1 month, 2 weeks ago by berry metal.
    Moderator bcworkz

    (@bcworkz)

    You last find relies upon being in the loop, but your block filter is out of scope, so you’re better off with the solution at https://stackoverflow.com/questions/3454258/php-date-yesterday-today

    Forget the last line of that solution. Place the offered function declaration in functions.php. Then where you want a date reference to appear, maybe after echoing out the title, add this:
    echo get_day_name( strtotime( $post->post_date ));

    Hi,

    would this solution work also if the site is cached (page cache,
    + cdn + reverse caching proxy + redis object caching)

    I couldn’t set it up yet so I couldn’t test this yet, but in theory would it bypass the caches?

    Thanks!

    • This reply was modified 1 month, 1 week ago by berry metal.
    Moderator bcworkz

    (@bcworkz)

    I don’t know enough about how those caches work to comment. I suppose it depends on how long content is cached. If for an hour or two it’s probably close enough. Much longer and I think it could be a problem. If your theory is the page would bypass caching because its content had changed, I don’t think so. If the page would bypass caching due to headers sent by WP, then maybe. Testing with your specific situation would be the best way to know for sure ??

    Hi, this is my final code:

    function get_day_name($timestamp) {
    
        $date = date('d/m/Y', $timestamp);
    
        if($date == date('d/m/Y')) {
          $date = 'Today';
        } 
        else if($date == date('d/m/Y',now() - (24 * 60 * 60))) {
          $date = 'Yesterday';
        }
        return $date;
    }
    
    function prefix_register_block( $blocks ) {
        
        // 'my_block' corresponds to the block slug.
        $blocks['my_block'] = [
            'name' => __( 'My Block', 'text-domain' ),
            'render_callback' => 'prefix_my_block_render',
        ];
    
        return $blocks;
        
    }
    
    add_filter( 'wp_grid_builder/blocks', 'prefix_register_block', 10, 1 );
    
    // The render callback function allows to output content in cards.
    function prefix_my_block_render() {
    
        // Get current post, term, or user object.
        $post = wpgb_get_post();
    
        // Output the post title.
        
        echo get_day_name( strtotime( $post->post_date ));
    
    }

    But it just makes my whole grid disappear.
    What could be the issue with the code?

    my_block is successfully displayed if I use the

    echo '<h3>' . esc_html( $post->post_title ) . '</h3>';

    code, so the error is within this code:

    echo get_day_name( strtotime( $post->post_date ));

    • This reply was modified 2 weeks, 5 days ago by berry metal.
    • This reply was modified 2 weeks, 5 days ago by berry metal.

    Or could be the function wrong?

    It’s solved:

    add_filter(
    	'wp_grid_builder/blocks',
    	function( $blocks ) {
    
    		$blocks['date_block'] = [
    			'name'            => 'Date Block',
    			'render_callback' => function() {
    
    				$date = get_post_timestamp( wpgb_get_the_id() );
    				$diff = (int) abs( time() - $date );
    
    				if ( $diff < DAY_IN_SECONDS ) {
    					echo 'Today';
    				} elseif ( $diff < DAY_IN_SECONDS * 2 ) {
    					echo 'Yesterday';
    				}
    			},
    		];
    
    		return $blocks;
    
    	}
    );

    However I need to clear the cache daily for this to work… ??

Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.