14 November 2023

The code snippet use the DateTime class to calculate the start and end timestamps for the previous quarter. The calculations take into account the current month and adjust it to correctly identify the previous quarter. The time is explicitly set to midnight (00:00:00.000000) for the start timestamp and the end of the day (23:59:59.999999) for the end timestamp.

Source code viewer
  1. // First day of previous quarter.
  2. $start = (new DateTime('first day of -' . (((date('n') - 1) % 3) + 3) . ' month'))
  3. ->modify('00:00:00.000000')
  4. ->getTimestamp();
  5.  
  6. // Llast day of previous quarter.
  7. $end = (new DateTime('last day of -' . (((date('n') - 1) % 3) + 1) . ' month'))
  8. ->modify('23:59:59.999999')
  9. ->getTimestamp();
Programming Language: PHP