Is there any libraries or ready to use functions for easy calculating timestamps in PHP?
I need to get:
- unixtimestamp: first second of current quarter,
- unixtimestamp: last second of current quarter,
- unixtimestamp: first second of previous quarter,
- unixtimestamp: last second of previous quarter,
Problems:
- timezones,
- begining of the year in example 3) and 4)
How do You calculate that? My code looks very complicated...
CodePudding user response:
I wrote a function for you.
function getTimestampFromQuarter(string $quarterName = 'current', string $returnTimestamp = 'start', string $timezone = 'UTC', int $year = null): int {
$dt = new DateTime();
$dt->setTimezone(new DateTimeZone($timezone));
// if year not defined, we use current year
$year = $year ?? date('Y');
// current month
$month = date("n");
// current quarter number
$quarter = ceil($month / 3);
// if we are looking for previous quarter, we have to do some math
if (in_array(strtolower($quarterName), ['previous','last'])) {
$quarter--;
if (0 == $quarter) {
$quarter = 4;
$year--;
}
}
$quarterFirstMonth = (12 / 4) * ($quarter - 1) 1; // first month of quarter
$quarterLastMonth = (12 / 4) * ($quarter - 1) 3; // last month of quarter
if ('start'==$returnTimestamp) {
$dt->setDate($year, $quarterFirstMonth, 1);
$dt->setTime(0, 0, 0);
}
elseif ('end'==$returnTimestamp) {
// looking for month days count
$ts = new DateTime();
$ts->setDate($year, $quarterLastMonth, 1);
$ts->setTimezone(new DateTimeZone($timezone));
$day = date('t', $ts->getTimestamp());
unset($ts);
$dt->setDate($year, $quarterLastMonth, $day);
$dt->setTime(23, 59, 59);
}
return $dt->getTimestamp();
}
Usage is easy:
1.. first second of current quarter:
echo getTimestampFromQuarter('current', 'start');
// output: 1633046400 (equals to Friday 1. October 2021 0:00:00)
2.. last second of current quarter:
echo getTimestampFromQuarter('current', 'end');
// output: 1640995199 (equals to Friday 31. December 2021 23:59:59)
3.. first second of previous quarter:
echo getTimestampFromQuarter('previous', 'start');
// output: 1625097600 (equals to Thursday 1. July 2021 0:00:00)
4.. last second of previous quarter:
echo getTimestampFromQuarter('previous', 'end');
// output: 1633046399 (equals to Thursday 30. September 2021 23:59:59)
CodePudding user response:
I gave DateTime a shot, and tried to make it easy to expand:
function getQuarterTimestamp(string $timestamp, string $returnTimestamp) : int {
// Offset to start of current quarter
$currentMonthQuarterOffset = - (date('n') 2) % 3;
switch($timestamp) {
case 'current':
break;
case 'previous':
$currentMonthQuarterOffset -= 3; // Move back 1 quarter
break;
default:
throw new InvalidArgumentException("Bad timestamp argument");
break;
}
switch($returnTimestamp) {
case 'start':
$dayOffset = 'first';
$timeOffset = '00:00:00';
break;
case 'end':
$currentMonthQuarterOffset = 2; // Default is start, move to end of quarter
$dayOffset = 'last';
$timeOffset = '23:59:59';
break;
default:
throw new InvalidArgumentException("Bad returnTimestamp argument");
break;
}
$dateTimeStr = "{$dayOffset} day of {$currentMonthQuarterOffset} month {$timeOffset}";
return (new DateTime($dateTimeStr))->getTimeStamp();
}