I have a submissions list and each one has its date_added, I am running also cron every day and checking if date_added is >= 30 day.
I am looking for a correct date check every day (cron) so if date_added is >= 30 day something must happen.
Date format: 2019-12-14 08:38:09
My code:
$afterMonth = date("Y-m-d H:i:s", strtotime("+1 months"));
Query Comparation:
$query->where($db->quoteName('last_updated') . ' >= ' . $db->quote($afterMonth));
I was just testing <= and it is working well, but have no idea how to test >= or if it is working.
suggest me better code, please as I am not strong in php.
Put the date logic in SQL, you won't need this line:
$afterMonth = date("Y-m-d H:i:s", strtotime("+1 months"));
Change your where
clause:
$query->where($db->quoteName('last_updated') . ' >= DATE_SUB(CURDATE(), INTERVAL 30 DAY');
We're using the DATE_SUB()
SQL function to subtract a timespan from a given date. We use CURDATE()
to get the current date and then INTERVAL 30 DAY
to set the timespan to subtract.
You could also just use:
$query->where($db->quoteName('last_updated') . ' >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH)');
More on this function here