Recently, I’ve been working on a project that requires a lot of work with dates. Depending on the nature of the work, there are times in which this can be easier than others.
On the one hand, if you’re making a simple comparison between, say, the values of two months then that’s not a big deal. On the other hand, if you’re dealing with months, days, years, the current date, timezone offsets, and different formats, then it can be a bit more complicated.
The point of all of the above, though, is not about strictly working with dates. Instead, it’s about the challenges that come with needing to write, test, and work with source code.
And if you’re in the habit of writing code, testing it in a browser, doing some form of debugging or dumping information onto a screen, and then repeating the process, there are easier ways to deal with this.
Using The PHP Interpreter
I’d venture to say that many people who work with PHP in a professional capacity are familiar with using the PHP interpreter, it’s something that is easy to forget when you’re working with web applications because you have the browser as a view and as a way to evaluate your code.
But if you have PHP installed, you have the interpreter installed. Assuming PHP is in globally available on your system, you can access it via the command-line by typing:
$ php -a
From here, you’ll be able to write PHP code and evaluate it whenever you begin interacting with the interpreter.
Though this can be a bit cumbersome when working with classes (and not something I recommend), but issuing commands and evaluating statements work great.
So what does this have to do with WordPress?
Bringing the post full circle, how might one work with date comparisons using information in the WordPress database for a given post and another date?
Perhaps you could:
- grab the value of the database column,
- store it in a string in the terminal,
- create your date value in the terminal,
- and perform the necessary operations for your testing.
Again, it’s not the only way to deal with this kind of stuff, but it’s one way. And having the PHP interpreter available can be a big help when you want to test some code on a proverbial paper napkin sketch essentially.