MySQL Tips on Date

mysql tips on date

While we work on some custom php or mysql projects then often we require to make transaction or actions reports based on dates and date range. Like Daily, yesterday’s, last seven day’s, last months, last 3 month’s etc. We can do that easily with sql queries describe in todays MySQL Tips on Date

mysql tips on date

MySQL Tips on Date

For Example: If we want to get all the orders which was placed yesterday,

order_placed_date = date_format(NOW() - INTERVAL 1 DAY, '%Y-%m-%d')

If we want to get all the orders which was placed last seven day:

order_placed_date BETWEEN DATE_FORMAT(NOW() - INTERVAL 7 Day, '%Y-%m-01')
AND DATE_FORMAT(NOW() ,'%Y-%m-%d')

If we want to get all the orders which was placed last month:

order_placed_date BETWEEN DATE_FORMAT(NOW() - INTERVAL 1 MONTH, '%Y-%m-01') AND DATE_FORMAT(NOW() ,'%Y-%m-01')
Similarly for last 3 month it will be like this:

order_placed_date BETWEEN DATE_FORMAT(NOW() - INTERVAL 3 MONTH, '%Y-%m-01') AND DATE_FORMAT(NOW() ,'%Y-%m-01')

So we hope theseĀ MySQL tips on date will help you in your projects. 

I am a dreamer! I had a dream of founding a company and get established there with my own team. But still now serving for others. Hope some day.. the dream will become TRUE. Find more here: Wordpress Developer

2 Comments on "MySQL Tips on Date"

  1. PhpFreak says:

    Handy tips.. Posted it as reference will help in future for own development purposes.. :)

    Mothers Day Gifts

  2. Jahanzeb says:

    Thanks for za tips!

Got something to say? Go for it!

*

 
More in Databases, Tips & Tricks (1 of 4 articles)