Joomla mysql date math logic

I am trying to create a condition via php and mysql that a user can submit in a maximum of 5 submissions within a 24 hour formid time period

// Define the maximum number of submissions for 24 hours
$max = 5;

$user = JFactory::getUser();
$db   = JFactory::getDbo();
$query   = $db->getQuery(true);

// Setup the query.
$query->select('COUNT('.$db->qn('Username').')')
    ->from($db->qn('#__rs_submissions'))
    ->where($db->qn('FormId').'='.$db->q($formId))
    ->where($db->qn('Username').'='.$db->q($user->get('username')));
    ->where($db->qn('DateSubmitted').'>='.$db->q('DATE_SUB(NOW(), INTERVAL 1 DAY)'))

$db->setQuery($query);
$counter = $db->loadResult();

if ($counter >= $max){
  $formLayout = '<p style="color:blue;">Oops !! Your Limits are Exhausted for 24 hours </p>';
}
}

Below are the values ​​of the database table enter image description here

Request does not comply with 24-hour date condition

 ->where($db->qn('DateSubmitted').'>='.$db->q('DATE_SUB(NOW(), INTERVAL 1 DAY)'))

can someone help and advise - what am I doing wrong and the solution to comply with the 24-hour limit

Edit I assume - the counter ($ db-> qn ('DateSubmitted'). '> ='. $ Db-> q ('DATE_SUB (NOW (), INTERVAL 1 DAY)')) should be executed - along with the last recorded time should be the basis for calculating the 24-hour period, still uncertain because it cannot achieve the desired

Launching Bounty - can someone help with this.

+4
2

. , :

DateSubmitted >= 'DATE_SUB(NOW(), INTERVAL 1 DAY)'

, . - , ( , ), . :

 ->where($db->qn('DateSubmitted'). ' >= DATE_SUB(NOW(), INTERVAL 1 DAY)')
+3

NOW CURDATE

- > ($ db- > qn ('DateSubmitted'). ' > ='. $db- > q ('CURDATE() - INTERVAL 1 DAY')

0

Source: https://habr.com/ru/post/1598551/


All Articles