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 ?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The 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.
Method 2
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0