So I'm revamping the entire core engine to my website CMS and I decided to use a single timestamp (UNIX epoch) instead of using the DATE and TIME datatypes that MySQL has. It seems a lot less bulky and there is more data contained in a single timestamp than two bulky columns for DATE and TIME.
I ran into a problem with this when I was coding the archived news section of my website (Now viewable
here). There was no real integrated way to select posts added in a specific month, so I had to think of some method.
Here's what I came up with:
I'd find the timestamp for the first day in the month (at 00:00:00) and the timestamp for the last day in the month (at 23:59:59) and all posts that had a timestamp that fell inbetween these two would have been posted within the month. Using PHP, I came up with a pretty good solution.
Toolbox: *
strtotime()*
date()* My database class (obviously you can substitute your own here)
(Note the variables $month and $year have already been calculated/specified):
// Get the UNIX timestamp for the first day in this month
$f_stamp = date('U', strtotime("$year-$month-1 00:00:00"));
// Get the UNIX timestamp for the last day in this month
$l_stamp = date('U', strtotime(date('F t, Y 23:59:59', strtotime("$year-$month-1"))));;
$db->sql_query("SELECT * FROM sidoh_news WHERE
`news_timestamp` >= $f_stamp AND
`news_timestamp` <= $l_stamp;");
As you can see by the link I provided, it works pretty well!
Just thought I'd share this with you all as I found it pretty useful.