Skip to content

Instantly share code, notes, and snippets.

@tuytoosh
Last active December 14, 2016 12:54
Show Gist options
  • Save tuytoosh/b7692d93f924e906bc28291e3f047be7 to your computer and use it in GitHub Desktop.
Save tuytoosh/b7692d93f924e906bc28291e3f047be7 to your computer and use it in GitHub Desktop.
بخش بندی بازه زمانی بر اساس روز - ماه - سال - دقیقه و ثانیه در کاربن / divide time period according to days , month , year and others...
<?php
public function index()
{
$start = '1992-5-7 11:25:17';
var_dump($this->times(20 , 'Year' , $start , false));
}
function times($number , $type = 'Day' , $start = null , $floor = true)
{
// $type can be => Second , Minute , Hour , Day , Month , Year
if($floor)
{
switch ($type)
{
case 'Second' :
$format = 'Y-m-d h:i:s';
break;
case 'Minute':
$format = 'Y-m-d h:i:00';
break;
case 'Hour' :
$format = 'Y-m-d h:00:00';
break;
case 'Day':
$format = 'Y-m-d 00:00:00';
break;
case 'Month':
$format = 'Y-m-00 00:00:00';
break;
case 'Year' :
$format = 'Y-00-00 00:00:00';
break;
default :
return false;
break;
}
}
else
$format = 'Y-m-d h:i:s';
$times = [];
$counter = 0;
$sub = 'sub' . $type;
$add = 'add' . $type;
if($start)
$step = Carbon::createFromFormat('Y-m-d h:i:s', $start);
else
$step = Carbon::now()->$sub($number);
while($counter < $number)
{
$times[$counter]['start'] = $step->format($format);
$times[$counter]['end'] = $step->$add()->format($format);
$counter++;
}
return $times;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment