dsqbh42082
dsqbh42082
2018-11-24 05:14
浏览 88
已采纳

带有变量的MySQL UNIX_TIMESTAMP?

Is there any way to pass a condition into UNIX_TIMESTAMP()?

eg : UNIX_TIMESTAMP(-24hr) <- this doesn't work

This returns all records that are newer than the current unix timestamp :

$sql = 'SELECT * FROM events WHERE event_date > UNIX_TIMESTAMP() ORDER BY event_date ASC';

But I would like to return all records that are newer than the current unix timestamp - 24hrs to allow for timezone differences.

Can it be done with the UNIX_TIMESTAMP() function alone?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doupian9490
    doupian9490 2018-11-24 05:19
    已采纳

    You can try below

    SELECT * FROM events 
     WHERE event_date > UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 24 Hour)) ORDER BY event_date ASC
    

    OR use shorthand version as @Madhur Bhaiya suggested

     SELECT * FROM events 
         WHERE event_date > UNIX_TIMESTAMP(NOW()- INTERVAL 24 Hour) ORDER BY event_date ASC
    
    点赞 评论

相关推荐