skip to Main Content

I have table with records. In table i have column ‘payment_date’, and it’s integer with unix timestamp. I need to select records from the database that are in the time interval of two days for each year and each month.enter image description here.

For example – I need all records from 1 to 15. This means that the result should contain entries for each year and each month from the 1st to the 15th:

from 2022-01-01 - to 2022-01-15
from 2022-02-01 - to 2022-02-15
//////////////////////
from 2023-07-01 - to 2023-07-15 etc.

2

Answers


  1. Chosen as BEST ANSWER

    $query->whereRaw('DAY(FROM_UNIXTIME(payment_date)) BETWEEN ' . $from . ' AND ' . $to);


  2. To retrieve records for each year and each month from the 1st to the 15th, you can use a combination of date functions like this:

    SELECT * FROM your_table WHERE FROM_UNIXTIME(payment_date) BETWEEN
    DATE_FORMAT(FROM_UNIXTIME(payment_date), ‘%Y-%m-01’) AND
    DATE_ADD(DATE_FORMAT(FROM_UNIXTIME(payment_date), ‘%Y-%m-01’),
    INTERVAL 14 DAY);

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search