Add one day to get_the_date in WordPress

In the WPTouch theme, the posting date is displayed in a large bubble. However, posts are added the night before, so the day is behind by one. To fix this, I changed this line:

<?php wptouch_the_time( 'j' ); ?>

to this:

Read More
    <?php
            $date = DateTime::createFromFormat('m-d-Y', mysql2date('m-d-Y', $post->post_date));
            $date->modify('+1 day');
            echo $date->format('j');
    ?>

This works, but is crazy ugly. I think there should be a better way to get from mysql date to php DateTime.

Related posts

Leave a Reply

3 comments

  1. You can use this filter to modify the date before it is given to the theme with the function get_the_date() (which wptouch_the_time() most probably uses to get the date):

    add_filter( 'get_the_date', 'modify_get_the_date', 10, 3 );
    function modify_get_the_date( $value, $format, $post ) {
        $date = new DateTime( $post->post_date );
        $date->modify( "+1 day" );
        if ( $format == "" )
            $format = get_option( "date_format" );
        return( $date->format( $format ) );
    }
    

    That being said, instead of modifying the date when it is read from the database, it would be better to just store the publication date when it is written. You can do this by scheduling posts for the next day, instead of publishing them directly.

  2. The way i always do this is using the strtotime method

    <?php 
    $format = ""; // your date format
    $oneDayLater = strtotime("+1 day", strtotime($columnValue));
    $out = date($format, $oneDayLater);
    ?>