Source Code

<?php

/* Today */
$todayStart =  new DateTime ( "midnight today" );
$todayEnd= new DateTIme ("midnight tomorrow");
echo 'Today Start: ' . $todayStart->format ( 'Y-m-d H:i:s' ) . '<br>';
echo 'Today End: ' . $todayEnd->format ( 'Y-m-d H:i:s' ) . '<br><br>';

/* Tomorrow */
$tomorrowStart = new DateTime ( "midnight tomorrow" );
$tomorrowEnd = clone $tomorrowStart;
$tomorrowEnd->modify ( '+1 day' );

echo 'Tomorrow Start: ' . $tomorrowStart->format ( 'Y-m-d H:i:s' ) . '<br>';
echo 'Tomorrow End: ' . $tomorrowEnd->format ( 'Y-m-d H:i:s' ) . '<br><br>';


/* Yesterday */
$yesterdayStart = new DateTime ( "midnight yesterday" );
$yesterdayEnd = clone $yesterdayStart;
$yesterdayEnd->modify ( '+1 day' );

echo 'Yesterday Start: ' . $yesterdayStart->format ( 'Y-m-d H:i:s' ) . '<br>';
echo 'Yesterday End: ' . $yesterdayEnd->format ( 'Y-m-d H:i:s' ) . '<br><br>';

/* Last Week */
$lastWeekStart = new DateTime ( "midnight monday last week" );
$lastWeekEnd = clone $lastWeekStart;
$lastWeekEnd->modify ( '+1 week' );

echo 'Last Week Start: ' . $lastWeekStart->format ( 'Y-m-d H:i:s' ) . '<br>';
echo 'Last Week End: ' . $lastWeekEnd->format ( 'Y-m-d H:i:s' ) . '<br><br>';

/* Two Weeks Ago */
$twoWeeksAgoStart = new DateTime ( "midnight monday 2 weeks ago" );
$twoWeeksAgoEnd = clone $twoWeeksAgoStart;
$twoWeeksAgoEnd->modify ( '+1 week' );

echo 'Two Weeks Ago Start: ' . $twoWeeksAgoStart->format ( 'Y-m-d H:i:s' ) . '<br>';
echo 'Two Weeks Ago End: ' . $twoWeeksAgoEnd->format ( 'Y-m-d H:i:s' ) . '<br><br>';

/* Three Weeks Ago */
$threeWeeksAgoStart = new DateTime ( "midnight monday 3 weeks ago" );
$threeWeeksAgoEnd = clone $threeWeeksAgoStart;
$threeWeeksAgoEnd->modify ( '+1 week' );

echo 'Three Weeks Ago Start: ' . $threeWeeksAgoStart->format ( 'Y-m-d H:i:s' ) . '<br>';
echo 'Three Weeks Ago End: ' . $threeWeeksAgoEnd->format ( 'Y-m-d H:i:s' ) . '<br><br>';

/* Last month */
$lastMonthStart = new DateTime ( "midnight first day of last month" );
$lastMonthEnd = clone $lastMonthStart;
$lastMonthEnd->modify ( '+1 month' );

echo 'Last Month Start: ' . $lastMonthStart->format ( 'Y-m-d H:i:s' ) . '<br>';
echo 'Last Month End: ' . $lastMonthEnd->format ( 'Y-m-d H:i:s' ) . '<br><br>';

echo '<h2>Quarters</h2>';
$firstQuarterStart = new DateTime ( 'first day of January' );
$firstQuaterEnd = clone $firstQuarterStart;
$firstQuaterEnd->modify ( "+3 months" );

echo 'First Quarter Start: ' . $firstQuarterStart->format ( 'Y-m-d H:i:s' ) . '<br>';
echo 'First Quarter End: ' . $firstQuaterEnd->format ( 'Y-m-d H:i:s' ) . '<br><br>';

$secondQuarterStart = new DateTime ( 'first day of April' );
$secondQuarterEnd = clone $secondQuarterStart;
$secondQuarterEnd->modify ( '+3 months' );

echo 'Second Quarter Start: ' . $secondQuarterStart->format ( 'Y-m-d H:i:s' ) . '<br>';
echo 'Second Quarter End: ' . $secondQuarterEnd->format ( 'Y-m-d H:i:s' ) . '<br><br>';

$thirdQuarterStart = new DateTime ( 'first day of July' );
$thirdQuarterEnd = clone $thirdQuarterStart;
$thirdQuarterEnd->modify ( '+3 months' );

echo 'Third Quarter Start: ' . $thirdQuarterStart->format ( 'Y-m-d H:i:s' ) . '<br>';
echo 'Third Quarter End: ' . $thirdQuarterEnd->format ( 'Y-m-d H:i:s' ) . '<br><br>';

$forthQuarterStart = new DateTime ( 'first day of October' );
$forthQuarterEnd = new DateTime ( 'first day of January next year' );

echo 'Forth Quarter Start: ' . $forthQuarterStart->format ( 'Y-m-d H:i:s' ) . '<br>';
echo 'Forth Quarter End: ' . $forthQuarterEnd->format ( 'Y-m-d H:i:s' ) . '<br><br>';

echo '<h2>Year To Date </h2>';
$ytdStart = new DateTime ( 'first day of January' );
$now = new DateTime ();

echo 'YTD Start: ' . $ytdStart->format ( 'Y-m-d H:i:s' ) . '<br>';
echo 'YTD End (now): ' . $now->format ( 'Y-m-d H:i:s' ) . '<br><br>';

This code is subject to change at any time.

Weeks and Days

Today Start: 2017-05-12 00:00:00
Today End: 2017-05-13 00:00:00

Tomorrow Start: 2017-05-13 00:00:00
Tomorrow End: 2017-05-14 00:00:00

Yesterday Start: 2017-05-11 00:00:00
Yesterday End: 2017-05-12 00:00:00

Last Week Start: 2017-05-01 00:00:00
Last Week End: 2017-05-08 00:00:00

Two Weeks Ago Start: 2017-04-17 00:00:00
Two Weeks Ago End: 2017-04-24 00:00:00

Three Weeks Ago Start: 2017-04-10 00:00:00
Three Weeks Ago End: 2017-04-17 00:00:00

Last Month Start: 2017-04-01 00:00:00
Last Month End: 2017-05-01 00:00:00

Quarters

First Quarter Start: 2017-01-01 00:00:00
First Quarter End: 2017-04-01 00:00:00

Second Quarter Start: 2017-04-01 00:00:00
Second Quarter End: 2017-07-01 00:00:00

Third Quarter Start: 2017-07-01 00:00:00
Third Quarter End: 2017-10-01 00:00:00

Forth Quarter Start: 2017-10-01 00:00:00
Forth Quarter End: 2018-01-01 00:00:00

Year To Date

YTD Start: 2017-01-01 00:00:00
YTD End (now): 2017-05-12 08:27:24

Disclaimer as to purpose

Some of you may have noticed that the ending dates all all wrong. Well, that depends entirely upon perspective.

Each of the dates shown should be understood to begin at the start of the second, whereas our assumption might be that the ending date would be at the end. For database queries this is typically not desired. Essentially, this can leave an entire second for each query unaccounted for. An eight-core, Xeon-based, dedicated server can process thousands of transactions within the missing second and a poorly considered / constructed date range will produce inaccurate reports.

What must be created is a means to include all of the transactions within a time frame which encompasses even those which are measured in the thousandths of the very last second in the query.

Database usage

The start date should be applied using a greater than or equal consideration.

The ending date should be applied using a straight-forward less than consideration.

Usages in xPDO

Coming soon....

In this article