WordPress – Display Current Date & Time Based on Website Timezone

I’m trying to display the current date and time on my WordPress website. I want the date and time to reflect the Timezone that has been set in the WordPress admin area.

I found this code in options-general.php:

Read More
<tr>
<th scope="row"><?php _e('Date Format') ?></th>
<td>
    <fieldset><legend class="screen-reader-text"><span><?php _e('Date Format') ?></span></legend>
<?php

    $date_formats = array_unique( apply_filters( 'date_formats', array(
        __('F j, Y'),
        'Y/m/d',
        'm/d/Y',
        'd/m/Y',
    ) ) );

    $custom = true;

    foreach ( $date_formats as $format ) {
        echo "t<label title='" . esc_attr($format) . "'><input type='radio' name='date_format' value='" . esc_attr($format) . "'";
        if ( get_option('date_format') === $format ) { // checked() uses "==" rather than "==="
            echo " checked='checked'";
            $custom = false;
        }
        echo ' /> <span>' . date_i18n( $format ) . "</span></label><br />n";
    }

    echo '  <label><input type="radio" name="date_format" id="date_format_custom_radio" value="custom"';
    checked( $custom );
    echo '/> ' . __('Custom:') . ' </label><input type="text" name="date_format_custom" value="' . esc_attr( get_option('date_format') ) . '" class="small-text" /> <span class="example"> ' . date_i18n( get_option('date_format') ) . "</span> <span class='spinner'></span>n";

    echo "t<p>" . __('<a href="http://codex.wordpress.org/Formatting_Date_and_Time">Documentation on date and time formatting</a>.') . "</p>n";
?>
    </fieldset>
</td>
</tr>

I tried adding:

<?php m/d/Y ?>

To my website code to output the current date but it didn’t work. What am I missing?

Related posts

Leave a Reply

2 comments

  1. Try with:

    $format = get_option('date_format') . ' ' . get_option('time_format');
    
    print date_i18n($format, current_time('timestamp'));
    

    WordPress has a current_time() function that returns the blog time, which might differ from the server time if you selected a different timezone in your blog settings…