I have a CPT for events. I need to display future events for people to register, while hiding past events.
What is the easiest date format for both (1) entering date when creating new event and (2) comparing event dates with current date ?
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Sign up to join this communityThe date format for entering the date is largely irrelevant. strtotime
will convert a lot of different formats to UNIX time. Just make sure your input format makes sense to your users and make sure that your feed strtottime
a format that correctly converts to UNIX time. If you are storing data in the *_postmeta
table, that is what I'd most likely do since ordinary math works on UNIX time so numeric comparisons work and because most of PHP's builtin date/time functions would require you to convert to UNIX time anyway. Even the MySQL database can work date logic using FROM_UNIXTIME
.
That isn't the only option. If you have your own table you could store in one of MySQL date/time formats and the database can work some date logic for you.
If you don't need any complex date logic you can store as YYYYMMDD and the date will sort correctly using ordinary math. That is is the only human date format that does sort correctly using ordinary math. It doesn't work well if you want to pick "all dates in May of any year" or all "January 1st events"-- that sort of thing.
I would suggest storing the dates in DATETIME
format as in YYYY-MM-DD HH:MM:SS
as it would then be easier to use MySQL to compare values.
As for the frontend user interface just convert it to DATETIME
format.