Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Formatting custom meta box date from YYYY/MM/DD to a more readable alternative

but when I visit a month with no posts my template returns the date
“January 1970” instead.

Yes. That will happen. UNIXTIME began on Jan 1, 1970. That is “0000/00/00” but negative numbers work back until sometime in 1901. strtotime will return false for anything outside that range, including your nonexistent dates. date will assume “day zero” if given a bad date, hence you get Jan 1, 1970 for nonexistent or otherwise flawed dates. 64 bit machines can handle larger ranges, if I remember correctly. Either way a nonexistent date will give you 1970. Try:

$calendar_month = "1901/01/01";
$this_month = strtotime($calendar_month);
var_dump($this_month);
echo '<br />';
echo date('Y',$this_month);
echo '<br />';

You want to make sure you have a good date before displaying it.

<?php $this_month = strtotime($calendar_month); 
if (false !== $this_month) { ?>
  <span id="current_month"><?php echo date( 'F Y', $this_month ); ?></span><?php
}

Your other code is a little bulky, but looks like it should work except that your date format is wrong. You aren’t getting “Month Day, Year” as I suspect you want but “Month-Name Month-Number, Year”, which will look like a duplicate if the two dates are in the same month/year. Take a good look at the date formatting operators.

$eventstart = get_post_meta($post->ID, 'epr_startdate', true);
$eventstart_col = strtotime($eventstart);

$eventend = get_post_meta($post->ID, 'epr_enddate', true);
$eventend_col = strtotime($eventend);

if ( $eventstart_col && !$eventend_col ) {
    // only the start date
    echo date( 'M d, Y', $eventstart_col );
} elseif ( $eventstart_col && $eventend_col ) {
    // both start and end date
    echo date( 'M d, Y', $eventstart_col ) . ' &mdash; ' . date( 'M d, Y', $eventend_col );  
} else {
    echo 'NA';
}

I think I have that put back together correctly.

Related Posts:

  1. Detect meta value changes when post is updated (post_updated)
  2. Displaying Meta Box Image
  3. Populate Custom Fields in a Custom Post Type?
  4. How to check if user meta field is empty in conditional else statement
  5. A better way to add a meta box to custom post types
  6. create custom meta box with default value
  7. How can I output WPAlchemy repeating fields meta values in my page template?
  8. Add a class to post_class if more than one post shares same meta_value_num
  9. Add a meta field to the list of results for a custom post type
  10. Empty meta-box returns publishdate if no value is set?
  11. Custom Post Type meta data getting deleted on bulk editing taxonomies
  12. Delete custom post type metadata without deleting the post in admin area
  13. Visual editor issue by having multiple tiny mce editors in a CPT
  14. Update Post Meta for a logged in user
  15. Found 2 elements with non-unique id (#_ajax_nonce) and (#_wpnonce)
  16. Plugin – Combine Meta Box Input Fields into single saveble record
  17. Two Custom Post Types Many to Many Relationship
  18. get_post_meta not working on publishing
  19. Meta Query Filtering not working on Custom Meta Box using Radio Buttons
  20. How do I set all of a particular post meta to a value within the custom post type I’m in?
  21. Grouping metadatas into one
  22. Metabox not show in categories custom post type cmb2
  23. Repeatable Fields Metabox with Textarea (or wp_editor)
  24. How to use TinyMCE Editor for one of my custom post meta field?
  25. Custom fields (wp_post_meta) vs Custom Table for large amount of data
  26. Show Custom Post Type meta boxes only on Page Edit
  27. Cannot save CPT meta box
  28. Adding Page Templates to post but it ignored it
  29. Displaying custom field according to date
  30. Importing Data from a Non-WordPress database, into WP
  31. Permalinks: custom structure for taxonomy – tags?
  32. Can’t publish custom post type – “You are not allowed to edit this post.”
  33. Custom fields for custom post type
  34. Custom “radio button meta box” not saving correctly
  35. Meta Data for Custom Post Type not saving
  36. Add additional field to custom post_type
  37. Custom Post Meta from custom metaboxes is randomly being deleted
  38. Check if post with same meta value exists
  39. Add a custom meta box for client to order CPT posts how they want
  40. Submitting Custom Post Types with custom fields from Front-end form
  41. Display Repeatable Meta Box Content
  42. Displaying a div from an assigned meta_value when on a page
  43. How to Echo Metadata Value in Currency Format
  44. Issue on Working with Metabox – Checkbox to enable Sale Items
  45. delete_post_meta() for whole CPT / multiple posts?
  46. How to get specific post meta by title or id
  47. What do the numbers mean at the end of add_action(‘save_post’)…?
  48. A meta box (in a custom post type) with two different type of fields
  49. Why do I have to press the “Update” button twice to save my meta box values?
  50. Values from meta-box not on $_POST
  51. Custom post type suddenly stoped working after WordPress 5 update
  52. copy images from custom field to another custom field
  53. How to show custom field on specific custom post type posts only when filled
  54. Reusable metabox backend and frontend
  55. Cannot obtain custom meta information
  56. Get custom term meta problem on single post type
  57. Display custom meta on page that has been check in custom post type
  58. Meta box data is saved but NOT displayed in the meta box text field. Why?
  59. How to call a post’s metadata in shortcode.php?
  60. How can I modify a custom post type and custom page template for a child theme if all content seams to be handled by theme’s ‘native’ plugin?
  61. Save Metabox Custom Field Value
  62. Incorrect ordering of custom post type based on time
  63. Meta box data not saving
  64. WP_Meta_Query object with conditionals
  65. Related posts meta_query CPT
  66. Meta box with front-end styling
  67. Sanitaizing Select Optin For Custom Post Type Metabox in WP
  68. How to retrive Custom Post Type Meta Fields in Custom WP_Query
  69. Cannot Save MetaBox Data in Custom Post Type
  70. Adding an extra parameter [string] to my posts’ permalink?
  71. delete duplicate meta_value with same post_id
  72. Search CPT Title AND Meta
  73. updating one custom meta field only
  74. Problem On Retrieving Post Meta Data on Custom Column [closed]
  75. Function to return values from metabox
  76. Saving metabox keys and storing values as array
  77. Custom Post Type Meta Box Text Input Field Won’t Save When Blank
  78. Filter search posts by post meta?
  79. Custom Meta Box with variable number of fields
  80. Custom fields for post or terms which don’t update on post update
  81. Unable to gather Image URL from Custom Post Type’s; Custom Meta Field
  82. How to keep custom post type related information
  83. How to avoid duplicate posts queried from custom meta?
  84. Custom Metabox Info Not Saving
  85. How can I add a meta[] to my custom post type and search by term with the Rest API?
  86. Show posts from WP Custom Post Type selected from a field in a metabox
  87. How can I fetch all the dates from custom fields from various different custom post types and show / list them at one place in ascending order?
  88. Creating an archive page or simple template to list all values of a custom field of specific post type listing
  89. Where is get_post_meta value located?
  90. PHP Warning with Custom Fields
  91. Custom meta fields not showing up in WP_Response Object via custom endpoint
  92. Problem with multi checkboxes value in metabox?
  93. Get meta values from parent post and save in child post
  94. How to Disable option of meta field if that option is selected for any other post in custom post type?
  95. Simple Data picker meta box
  96. Metaboxes in Loop
  97. Add more custom fields when creating a new custom post type
  98. check_admin_referer not working in custom meta box for custom post type
  99. Custom meta box data not saving
  100. How do I extract the contents of a CPT’s custom field for all posts?
Categories custom-post-types Tags custom-field, custom-post-types, date-time, metabox, post-meta
Check if current page is wp-admin
How to add enctype to multipart/form-data to comment form?

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress