interval()

Builds a MySQL-compatible INTERVAL expression for use in date comparisons. Useful in WHERE clauses and subqueries involving relative time.

Signature:

public function interval(int $amount, string $unit = 'DAY'): string

Example: get entries from last 7 days

// Get orders from last 7 days
$db->whereFunc('order_date', ['[F]' => "NOW() - INTERVAL 7 DAY"], '>=');
$orders = $db->get('orders');

Or using the interval() helper

// Same as above, but cleaner
$interval = $db->interval(7, 'DAY');
$db->whereFunc('order_date', ['[F]' => "NOW() - $interval"], '>=');
$orders = $db->get('orders');
Tip: You can also use other units like HOUR, MINUTE, MONTH, or YEAR. Just change the second parameter.

Valid Interval Units

  • SECOND
  • MINUTE
  • HOUR
  • DAY
  • WEEK
  • MONTH
  • YEAR

See Also