SQL Date Interval

Data set:

id created_date
1 2018-01-12 14:55:00
2 2018-01-13 23:12:39
3 2018-02-12 07:07:50
4 2018-02-12 16:40:01
5 2018-02-13 06:52:16
6 2018-02-13 10:23:35
7 2018-02-14 18:49:32
8 2018-02-15 04:37:19
9 2018-02-15 11:17:18

Assumption:
Current date time is 13 February 2018, 07:50am.

  1. Get past events till 24 hours before current date time

    		SELECT * FROM table
    		WHERE created_date < DATE_SUB(NOW(), INTERVAL 24 HOUR) 
    		

    Output:

    id created_date
    1 2018-01-12 14:55:00
    2 2018-01-13 23:12:39
    3 2018-02-12 07:07:50
  2. Get past events till a day before current date time

    	SELECT * FROM table
    	WHERE created_date < NOW() - INTERVAL 1 DAY
    	

    Output:

    id created_date
    1 2018-01-12 14:55:00
    2 2018-01-13 23:12:39
    3 2018-02-12 07:07:50
  3. Get events 24 hours before current date time till 24 hours from current date time

    	SELECT * FROM table
    	WHERE created_date > DATE_SUB(NOW(), INTERVAL 24 HOUR)
    	AND created_date < DATE_ADD(NOW(), INTERVAL 24 HOUR)
    	

    Output:

    id created_date
    4 2018-02-12 16:40:01
    5 2018-02-13 06:52:16
    6 2018-02-13 10:23:35
  4. Get events within 24 hours

    	SELECT * FROM table
    	WHERE created_date > DATE_SUB(NOW(), INTERVAL 12 HOUR) 
    	AND created_date < DATE_ADD(NOW(), INTERVAL 12 HOUR)
    	

    Output:

    id created_date
    5 2018-02-13 06:52:16
    6 2018-02-13 10:23:35
  5. Get future events at least 2 to 24 hours from the current date time

    	SELECT * FROM table
    	WHERE created_date > DATE_ADD(NOW(), INTERVAL 2 HOUR)
    	AND created_date < DATE_ADD(NOW(), INTERVAL 24 HOUR)
    	

    Output:

    id created_date
    6 2018-02-13 10:23:35
  6. Get future events at least 1 day later from current date time

    	SELECT * FROM table
    	WHERE created_date >= NOW() + INTERVAL 1 DAY
    	

    Output:

    id created_date
    7 2018-02-14 18:49:32
    8 2018-02-15 04:37:19
    9 2018-02-15 11:17:18
  7. Get future events from at least 2 days later from current date time

    	SELECT * FROM table
    	WHERE created_date >= NOW() + INTERVAL 2 DAY
    	

    Output:

    id created_date
    9 2018-02-15 11:17:18

Goodbye 2017!

I hardly do a full speech of my new year resolution but 2017 is a sentimental year. This year is my first full year working after graduation. It’s a new experience and I learnt a lot from being a working adult. I am thankful that I have many friends, new friends and colleagues-who-turned-friends. I hope in 2018, these friendships continue and become stronger.

This year, my manager left and that affected me awhile. But because of that incident, I learnt a lot and did things that I never thought I’d be able to. I discovered that I truly love doing programming. When I talk to my friends about my work, it is always about my problem-solving skills, accomplishment (and boss). Doing what you love is a blessing. In 2018, I hope that my passion continues to grow and learning never stops.

This year, dearest Russell left us. I’ve known best friend’s ah dog for more than a decade. I have photos of him more than best friend and I and he was like a friend to my pets. In 2018, it will be a different year without him but let’s cope and do it well. We still have each other to grow old together and become cute grandmas.

This year, I am especially grateful to my family. They have always been my faithful listening ears. Many times I was stuck at work but I know they are there to support me. My siblings showed me that I don’t have to be the only pillar as they can take up the role too. In 2018, I will work towards being an even more magnanimous sister, buy more good food, and sponsor more things for our nest.

This year, I’ve travelled to two countries. I hope in 2018, I will earn more and save more so that I can go even further to see the World.

In 2018, I also hope to watch more movies, start reading again, eat more good food, have a healthy lifestyle, and be healthy. All in all, I hope that the new year will be even more awesome than the past year!