📜 ⬆️ ⬇️

Determination of time periods on the current date

Somehow recently there was a task to make samples from the database for different periods of time, depending on the current date. For these purposes, I implemented a class with a certain set of functions defining the end sections of the necessary time intervals.

In my opinion, such tasks arise quite often and it is very convenient to have one tool that can later be supplemented with new functions corresponding to different time intervals. Of course, there is nothing difficult in this, but I think it will help someone save time on this routine work. In addition, in the net nothing I just did not find.
/**
* Class for making different periods of date without time.
*
* @author Anton Vasilyev <anton.vasilyev@gtmdevelopments.com>
*/
class DatePeriod
{
/**
* Unix timestamp for date without time.
*
* @var int
*/
private $_timestamp;

/**
* Year.
*
* @var int
*/
private $_year;

/**
* Month.
*
* @var int
*/
private $_month;

/**
* Day.
*
* @var int
*/
private $_day;

/**
* Number of seconds for one day.
*/
const DAY_SECONDS = 86400;

/**
* Constructor.
*
* @param string $date String containing a US English date format (used in function strtotime).
* @author Anton Vasilyev <anton.vasilyev@gtmdevelopments.com>
*/
public function __construct($date = null )
{
// Using today

if ($date == null ) {
$ this ->_timestamp = strtotime(date( 'Ym-d' ));
} else {
$ this ->_timestamp = strtotime($date);
}

$ this ->_day = date( 'j' , $ this ->_timestamp);
$ this ->_month = date( 'n' , $ this ->_timestamp);
$ this ->_year = date( 'Y' , $ this ->_timestamp);
}

/**
* Calculates start and end date of week.
* Returns array with two values: from date and to date.
*
* @param string $format Format of the date (used in function date).
* @return array Array with two keys: from and to. Example:
* array(
* 'from' => ...
* 'to' => ...
* )
* @author Anton Vasilyev <anton.vasilyev@gtmdevelopments.com>
*/
public function getWeek($format)
{
$dayOfWeek = date( 'w' , $ this ->_timestamp);

return array(
'from' => date($format, $ this ->_timestamp - $dayOfWeek*self::DAY_SECONDS),
'to' => date($format, $ this ->_timestamp + (6 - $dayOfWeek)*self::DAY_SECONDS)
);
}

/**
* Calculates start and end date of month.
* Return array with two values: from date and to date.
*
* @param string $format Format of the date (used in function date).
* @return array Array with two keys: from and to. Example:
* array(
* 'from' => ...
* 'to' => ...
* )
* @author Anton Vasilyev <anton.vasilyev@gtmdevelopments.com>
*/
public function getMonth($format)
{
$monthDays = date( 't' , $ this ->_timestamp);

return array(
'from' => date($format, strtotime($ this ->_year . '-' . $ this ->_month . '-01' )),
'to' => date($format, strtotime($ this ->_year . '-' . $ this ->_month . '-' . $monthDays))
);
}

/**
* Calculates start and end date of current quarter.
* Return array with two values: from date and to date.
*
* @param string $format Format of the date (used in function date).
* @return array Array with two keys: from and to. Example:
* array(
* 'from' => ...
* 'to' => ...
* )
* @author Anton Vasilyev <anton.vasilyev@gtmdevelopments.com>
*/
public function getCurrentQuarter($format)
{
$quarter = ( int )($ this ->_month/4) + 1;

$fromMonth = ($quarter - 1)*3 + 1;
$toMonth = $quarter*3;

// Number of days in last quarter month.

$daysInToMonth = date( 't' , strtotime($ this ->_year . '-' . $toMonth . '-01' ));

return array(
'from' => date($format, strtotime($ this ->_year . '-' . $fromMonth . '-01' )),
'to' => date($format, strtotime($ this ->_year . '-' . $toMonth . '-' . $daysInToMonth))
);
}

/**
* Calculates start and end date of previous quarter.
* Return array with two values: from date and to date.
*
* @param string $format Format of the date (used in function date).
* @return array Array with two keys: from and to. Example:
* array(
* 'from' => ...
* 'to' => ...
* )
* @author Anton Vasilyev <anton.vasilyev@gtmdevelopments.com>
*/
public function getPrevQuarter($format)
{
$quarter = ( int )($ this ->_month/4) + 1;

if ($quarter == 1) {
$quarter = 4;
$year = $ this ->_year - 1;
} else {
$quarter -= 1;
$year = $ this ->_year;
}


$fromMonth = ($quarter - 1)*3 + 1;
$toMonth = $quarter*3;

// Number of days in last quarter month.

$daysInToMonth = date( 't' , strtotime($year . '-' . $toMonth . '-01' ));

return array(
'from' => date($format, strtotime($year . '-' . $fromMonth . '-01' )),
'to' => date($format, strtotime($year . '-' . $toMonth . '-' . $daysInToMonth))
);
}

/**
* Calculates start and end date of current calendar year to date.
* Return array with two values: from date and to date.
*
* @param string $format Format of the date (used in function date).
* @return array Array with two keys: from and to. Example:
* array(
* 'from' => ...
* 'to' => ...
* )
* @author Anton Vasilyev <anton.vasilyev@gtmdevelopments.com>
*/
public function getYearToDate($format)
{
return array(
'from' => date($format, strtotime($ this ->_year . '-01-01' )),
'to' => date($format, $ this ->_timestamp)
);
}

/**
* Calculates start and end date of the last calendar year.
* Return array with two values: from date and to date.
*
* @param string $format Format of the date (used in function date).
* @return array Array with two keys: from and to. Example:
* array(
* 'from' => ...
* 'to' => ...
* )
* @author Anton Vasilyev <anton.vasilyev@gtmdevelopments.com>
*/
public function getLastYear($format)
{
return array(
'from' => date($format, strtotime($ this ->_year - 1 . '-01-01' )),
'to' => date($format, strtotime($ this ->_year - 1 . '-12-31' ))
);
}

/**
* Calculates start and end date of last calendar year to date.
* Return array with two values: from date and to date.
*
* @param string $format Format of the date (used in function date).
* @return array Array with two keys: from and to. Example:
* array(
* 'from' => ...
* 'to' => ...
* )
* @author Anton Vasilyev <anton.vasilyev@gtmdevelopments.com>
*/
public function getTwelveMonths($format)
{
$fromMonth = (12 + ($ this ->_month - 11))%12 == 0 ? 1 : (12 + ($ this ->_month - 11))%12;

if ($fromMonth > 1) {
$year = $ this ->_year - 1;
} else {
$year = $ this ->_year;
}

// Number of days in last quarter month.

$daysInToMonth = date( 't' , strtotime($ this ->_year . '-' . $ this ->_month . '-01' ));

return array(
'from' => date($format, strtotime($year . '-' . $fromMonth . '-01' )),
'to' => date($format, $ this ->_timestamp)
);
}
}


* This source code was highlighted with Source Code Highlighter .

')

Source: https://habr.com/ru/post/49553/


All Articles