Today, I was asked to make a query which shows only the posts that has been posted yesterday or earlier.
This was because a graduated guy was "unable to find a function" to do this (!).
Here is a small script how it can be done using easy functions and logic:
1) Find the exact day, month and year of today.
$day = date("d", time()); // date('d'); will also work but I am using the second parameter if needs to change timezone or I don't know
$month = date("m", time());
$year = date("Y", time());
2) Knowing that the day begins at 00:00 (midnight), we need to convert this hour into time() format, so we can deal later with it.
$today = mktime("00", "00", "00", "$month", "$day", "$year");
The above function, gives to the variable $today the exact value in seconds which means the time: 00hours, 00minutes, 00seconds, $day days, $month months, $year years. The $day, $month and $year were filled in the step 1.
3) Now, to complete our mission, the logic is like this:
Select something from somewhere, where the entry date is lower as a numeric value, than today.
Remember that today started 00:00. Even one second before this time, it makes it "yesterday".
So here is the final query:
SELECT something FROM somewhere WHERE entry_date < '$today'
---
In my case, the code was:
$today = mktime("00", "00", "00", date('m'), date('d'), date('y'));
This was because a graduated guy was "unable to find a function" to do this (!).
Here is a small script how it can be done using easy functions and logic:
1) Find the exact day, month and year of today.
$day = date("d", time()); // date('d'); will also work but I am using the second parameter if needs to change timezone or I don't know
$month = date("m", time());
$year = date("Y", time());
2) Knowing that the day begins at 00:00 (midnight), we need to convert this hour into time() format, so we can deal later with it.
$today = mktime("00", "00", "00", "$month", "$day", "$year");
The above function, gives to the variable $today the exact value in seconds which means the time: 00hours, 00minutes, 00seconds, $day days, $month months, $year years. The $day, $month and $year were filled in the step 1.
3) Now, to complete our mission, the logic is like this:
Select something from somewhere, where the entry date is lower as a numeric value, than today.
Remember that today started 00:00. Even one second before this time, it makes it "yesterday".
So here is the final query:
SELECT something FROM somewhere WHERE entry_date < '$today'
---
In my case, the code was:
$today = mktime("00", "00", "00", date('m'), date('d'), date('y'));