Skip to content

Instantly share code, notes, and snippets.

@marlenesco
Last active February 1, 2018 14:20
Show Gist options
  • Save marlenesco/b3d4ebb508293e2a7b240dbbcc06dc97 to your computer and use it in GitHub Desktop.
Save marlenesco/b3d4ebb508293e2a7b240dbbcc06dc97 to your computer and use it in GitHub Desktop.
Get date range for week or month
<?php
/**
* Created by PhpStorm.
* User: david.foliti
* Date: 01/02/18
* Time: 11:46
*/
namespace CalendarDipBundle\Utils;
class DateUtils
{
private $dateRange;
/**
* Get array Date range for a $week and $year
*
* @param null $week
* @param null $year
* @return array
*/
public function getWeekRange($week = null, $year = null)
{
if ($week === null) {
$today = new \DateTime(date('Y-m-d'));
$week = $today->format('w');
}
if ($year === null) {
$year = date('Y');
}
for ($i = 1; $i < 8; $i++) {
$date = new \DateTime();
$this->dateRange[] = $date->setISODate($year, $week, $i)->setTime(9, 0, 0);
}
return $this->dateRange;
}
/**
* Get array Date range for a $month and $year
*
* @param null $month
* @param null $year
* @return array
*/
public function getMonthRange($month = null, $year = null)
{
if ($month === null) {
$month = date('m');
}
if ($year === null) {
$year = date('Y');
}
$dayCount = cal_days_in_month(CAL_GREGORIAN, $month, $year);
for ($i = 1; $i <= $dayCount; $i++) {
$day = $year . '-' . $month . '-' . $i;
$date = new \DateTime($day);
$this->dateRange[] = $date->setTime(9, 0, 0);
}
return $this->dateRange;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment