Skip to content

Instantly share code, notes, and snippets.

@handhikadj
Last active March 4, 2019 04:38
Show Gist options
  • Save handhikadj/de129688720f7a38d6bee6958f2b0be6 to your computer and use it in GitHub Desktop.
Save handhikadj/de129688720f7a38d6bee6958f2b0be6 to your computer and use it in GitHub Desktop.
<?php
class PeriodSelf
{
public static function make($startTime = null, $endTime = null, $intervalCodeName, $isFullTime = 'fullTime')
{
$end = new \DateTime($endTime);
$start = clone $end;
$start->modify($startTime);
$interval = new \DateInterval($intervalCodeName);
$period = new \DatePeriod($start, $interval, $end);
foreach ($period as $date) {
$arrOfDate[] = $date;
}
if (isset($isFullTime) && $isFullTime != 'notFullTime') $arrOfDate[] = new \DateTime($endTime);
return $arrOfDate;
}
}
/**
Usage:
return PeriodSelf::make('-7 days', null, 'P1D');
results:
[
{
"date": "2019-02-25 11:34:58.842454",
"timezone_type": 3,
"timezone": "Asia/Jakarta"
},
{
"date": "2019-02-26 11:34:58.842454",
"timezone_type": 3,
"timezone": "Asia/Jakarta"
},
{
"date": "2019-02-27 11:34:58.842454",
"timezone_type": 3,
"timezone": "Asia/Jakarta"
},
{
"date": "2019-02-28 11:34:58.842454",
"timezone_type": 3,
"timezone": "Asia/Jakarta"
},
{
"date": "2019-03-01 11:34:58.842454",
"timezone_type": 3,
"timezone": "Asia/Jakarta"
},
{
"date": "2019-03-02 11:34:58.842454",
"timezone_type": 3,
"timezone": "Asia/Jakarta"
},
{
"date": "2019-03-03 11:34:58.842454",
"timezone_type": 3,
"timezone": "Asia/Jakarta"
},
{
"date": "2019-03-04 11:34:58.842477",
"timezone_type": 3,
"timezone": "Asia/Jakarta"
}
]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment