Formatting Post Dates in WordPress Without Time
When retrieving recent posts via wp_get_recent_posts(), you're likely hitting a common snag: the raw post_date string includes both date and time (e.g., 2013-08-11 18:29:04). If your sidebar or widget displays this directly, it clutters the UI with unnecessary precision. The solution lies in formatting the output to match your design needsâlike stripping the time and rearranging the date order to 8-11-2013.
WordPress stores dates as Unix timestamps internally but returns them as formatted strings depending on your date_format setting. When you echo $recent["post_date"], WordPress uses the default format defined in your theme options (often via get_option('date_format')). To override this, use PHP's built-in date() function with a custom format string.
Extracting and Formatting the Date
To remove the time and reorder the date, you can parse the stored timestamp and reformat it. Here's how to update your loop:
$args = array( 'numberposts' => 3 );
$recent_posts = wp_get_recent_posts( $args );
foreach ( $recent_posts as $recent ) {
// Get the post date in a custom format: Day-Month-Year
$formatted_date = date_i18n('m-d-Y', strtotime($recent['post_date']));
echo '<li id="sidebar_text"><b>' . esc_html( $recent['post_title'] ) . '</b></li>';
echo '<li style="font-size:12px">' . esc_html( $formatted_date ) . '</li>';
echo '<li><i style="font-size:15px">' . esc_html( $recent['post_excerpt'] ) . '</i>';
echo '<a href="' . esc_url( get_permalink($recent["ID"]) ) . '">Read More</a></li>';
}
The key change is replacing $recent["post_date"] with date_i18n('m-d-Y', strtotime($recent['post_date'])). The date_i18n() function respects WordPress's localization settings, ensuring the date format adapts to your site's locale if needed. Using strtotime() converts the stored date string into a Unix timestamp, which date_i18n() then formats according to your pattern.
Why Use date_i18n Over date?
While date() works in most cases, date_i18n() is preferred in WordPress contexts because it automatically handles locale-specific formatting. For instance, some regions expect "DD-MM-YYYY" instead of "MM-DD-YYYY". If your site is hosted in a region where day-first format is standard, using date_i18n('d-m-Y', ...) would be more appropriate.
Important Considerations
- Escaping Output: Always use
esc_html()oresc_url()when echoing user-generated content like titles, excerpts, and permalinks to prevent XSS vulnerabilities. - Timestamp Reliability: The
post_datefield is stored as a Unix timestamp in seconds with UTC timezone. If your server's timezone differs from your site's display timezone, consider adjusting the timestamp before formatting usingstrtotime()orgetdate(). - Post Date vs. Modified Date: Ensure you're formatting
post_date, notpost_modified. The latter reflects when a post was last edited, which may differ significantly from the original publication date.
Alternative: Using get_the_time() in Templates
If you were working within a template file like sidebar.php instead of a custom loop, you could also use get_the_time('m-d-Y'). However, since your code is inside a PHP block manually iterating posts, the date_i18n() approach remains the most direct and reliable.
By applying these techniques, you regain full control over how dates appear in your frontend, improving both aesthetics and user experience without compromising functionality.