Announcement

Collapse
No announcement yet.

Format Where Clause with comparison interval in ORM

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Format Where Clause with comparison interval in ORM

    Hello,

    The following code does not work. How do I create this query using ORM syntax ?

    PHP Code:
    ->where([
    'evaluationDate>' => 'DATE_SUB(NOW(),INTERVAL 11 YEAR)',
    ])
    ​ 
    Currently, this query gives me the following error:

    ERROR: (HY000) SQLSTATE[HY000]: General error: 1525 Incorrect DATETIME value: 'DATE_SUB(NOW(),INTERVAL 11 YEAR))'

    This is odd to me since the following code works directly in mysql

    Code:
    SELECT
    *
    FROM
    `equipment_performance_evaluation_report`
    WHERE
    `evaluation_date` > DATE_SUB(NOW(), INTERVAL 11 YEAR)​

  • #2
    Advice, do it in PHP, pass a result string-date.

    Comment


    • yuri
      yuri commented
      Editing a comment
      'evaluationDate>' => (new \DateTimeImmutable)->modify('-11 years')->format('Y-m-d H:i:s'),

  • #3
    Thanks for the suggestion. I thought maybe something clever like complex expression TIMESTAMPDIFF_YEAR could be useful and tried building Expression with it but Expression::TIMESTAMPDIFF_YEAR does not exist. I didn't know about DateTimeImmutable but that is useful. I ended up just doing it in PHP the old-fashioned way.

    PHP Code:
    $retentionDate date("Y-m-d"strtotime("-11 years")); 
    PHP Code:
    ->where([
    'evaluationDate>' => $retentionDate,
    ])
    ​ 

    Comment

    Working...
    X