DatePeriod returning empty object - php

Using code from this post https://stackoverflow.com/a/4312630/257629
I am getting an empty object when attempting to use DatePeriod(). My PHP is version 5.4.3 and I can't see any errors. The DateTime and DateInterval appear to return the correct objects, but when passing it to the DatePeriod, I am left with an empty object. (debug is from CakePHP and outputs the contents of the variable.)
// values passed from form, to a function
// $arrival = 2013-09-05
// $departure = 2013-08-16
$start = new DateTime($arrival);
/*
object(DateTime) {
date => '2013-09-05 00:00:00'
timezone_type => (int) 3
timezone => 'UTC'
}
*/
$interval = new DateInterval('P1D');
/*
object(DateInterval) {
y => (int) 0
m => (int) 0
d => (int) 1
h => (int) 0
i => (int) 0
s => (int) 0
invert => (int) 0
days => false
}
*/
$end = new DateTime($departure);
/*
object(DateTime) {
date => '2013-08-16 00:00:00'
timezone_type => (int) 3
timezone => 'UTC'
}
*/
$period = new DatePeriod($start, $interval, $end);
debug($period);
/*
object(DatePeriod) {
}
*/
foreach ($period as $date) {
echo $date->format('Y-m-d')."\n";
}

$arrival = 2013-09-05
$departure = 2013-08-16
Arrival is not greater than Departure. If you set $arrival = 2013-08-05. Then output will be
2013-08-05
2013-08-06
2013-08-07
2013-08-08
2013-08-09
2013-08-10
2013-08-11
2013-08-12
2013-08-13
2013-08-14
2013-08-15

$end date is before $start date because you mixed up $arrival and $departure vars

Related

Make 5 minute interval

I'm trying to separate the dates for example:
$arr=array(
"2018-06-27 20:30:20",
"2018-06-27 20:31:20",
"2018-06-27 20:37:20",
"2018-06-27 20:45:20",
"2018-06-27 20:48:20"
);
As you can see there are minutes with difference only of 1 minute or even seconds.
What I'm trying to accomplish is to force the dates to be 5 mins interval.
example output
2018-06-27 20:30:00
2018-06-27 20:35:00
2018-06-27 20:40:00
2018-06-27 20:45:00
2018-06-27 20:50:00
Here's my code
function roundToNearestMinuteInterval($time)
{
$time = (round(strtotime($time) / 300)) * 300;
return date('Y-m-d H:i:s', $time);
}
$temp="";
$wave=1;
foreach($arr as $a) {
if(empty($temp)) {
$temp= roundToNearestMinuteInterval($a);
}
$date= roundToNearestMinuteInterval($a);
if($temp==$date && $wave!=1){
$new=date('Y-m-d H:i:s',strtotime('+3 minutes',strtotime($a)));
$date= roundToNearestMinuteInterval($date);
$temp= $date;
}
$wave++;
echo $date."<br/>";
}
If you want to have an output array with all the 5 minute (or other interval) times between the earliest and latest times in the input array, you can just iterate between them, adding the interval in each loop:
$arr=array("2018-06-27 20:30:20","2018-06-27 20:31:20","2018-06-27 20:37:20","2018-06-27 20:45:20","2018-06-27 20:48:20");
function roundToNearestMinuteInterval($time, $interval) {
$timestamp = strtotime($time);
$rounded = round($timestamp / ($interval * 60), 0) * $interval * 60;
return $rounded;
}
$interval = 5; // minutes
$start = roundToNearestMinuteInterval(min($arr), $interval);
$end = roundToNearestMinuteInterval(max($arr), $interval);
for (; $start <= $end; $start += $interval * 60) {
$results[] = date('Y-m-d H:i:s', $start);
}
print_r($results);
Output:
Array
(
[0] => 2018-06-27 20:30:00
[1] => 2018-06-27 20:35:00
[2] => 2018-06-27 20:40:00
[3] => 2018-06-27 20:45:00
[4] => 2018-06-27 20:50:00
)
Demo on 3v4l.org
Solution with a DateTime extension dt (https://github.com/jspit-de/dt) returns an array with the date as a key. The value supplied is the number of rounded values ​​from the input array. The algorithm can be implemented even without class extension with a few more commands.
$inputArr = array(
"2018-06-27 20:30:20",
"2018-06-27 20:31:20",
"2018-06-27 20:37:20",
"2018-06-27 20:45:20",
"2018-06-27 20:48:20"
);
$interval = "5 Minutes";
//create basis
$resArr = [];
$dt = dt::create(min($inputArr))->round($interval); //start
$endDate = dt::create(max($inputArr))->round($interval);
for(;$dt <= $endDate; $dt->modify($interval)){
$key = $dt->format("Y-m-d H:i:s");
$resArr[$key] = 0;
}
foreach($inputArr as $strDate){
$key = $dt::create($strDate)
->round($interval)
->format("Y-m-d H:i:s");
$resArr[$key]++;
}
The result $resArr
array (
'2018-06-27 20:30:00' => 2,
'2018-06-27 20:35:00' => 1,
'2018-06-27 20:40:00' => 0,
'2018-06-27 20:45:00' => 1,
'2018-06-27 20:50:00' => 1,
)

Calculate Appointments beetween two dates

Situation:
I have arrays with information of the used appointments and the calculated days for the new appointment series based on PHP.
For example, the exist appointment array:
Array
(
[0] => Array
(
[date] => 2019-05-02
[time_start] => 14:00:00
[time_end] => 15:30:00
)
[1] => Array
(
[date] => 2019-05-06
[time_start] => 14:00:00
[time_end] => 15:30:00
)
)
Now, i will check have the calculated series (same array format) collisions with the exist appointments.
My Question:
How can i check if a collision exist beetween the start and end time and if yes how i can give the array a new value with a time windows after or before the exist appointment. This within a time windows from 08:00 am to 4:00 pm.
What i have is the calculation of the appointment days.
private function calculate_dates($data, $measure)
{
$this->load->model('calendar/calendar_model');
$holiday_dates = $this->calendar_model->get_holidays();
foreach ($holiday_dates as $key => $value) {
$holidays[] = $value['holiday_date'];
}
$begin = new DateTime($data->measure_begin);
$end = new DateTime($data->measure_end);
$oneday = new DateInterval("P1D");
$days = json_decode($data->measure_dates);
$wdays = array();
$ue_minutes = 0;
$minutes = ($data->measure_ue * $measure->ue_minutes/2);
$daterange = new DatePeriod( $begin, DateInterval::createFromDateString('+1 weekdays') ,$end );
foreach(new DatePeriod($begin, $oneday, $end->add($oneday)) as $day) {
$day_num = $day->format("N"); /* 'N' number days 1 (mon) to 7 (sun) */
if($day_num < 6 ) { /* weekday */
$wdays[] = $day;
}
}
$count = 1;
foreach($wdays as $date){
foreach ($days as $key => $value) {
if(mb_strtolower($date->format('l')) == $value){
if(($data->measure_ue/2)+1 != $count){
if(in_array($date->format('Y-m-d'), $holidays)) {
$dates[] = $this->close_days($date, $days, true)->format('l, d.m.Y');
} else {
$dates[] = $date->format('l, d.m.Y');
}
$count++;
}
}
}
}
return array(
'dates' => $dates,
'minutes' => round($minutes/count($dates))
);
}
private function close_days($date, $days, $init = false)
{
if($init){
$days[] = 'saturday';
$days[] = 'sunday';
}
if( in_array(mb_strtolower($date->format('l')), $days) ) {
$this->close_days($date->modify('next day'), $days);
}
return $date;
}
Any Ideas for a solution or maybe a code for a better way?

PHP - Date range by month

I have a date range and I need it to group by month but I want to keep starting day and ending day. So far I have this:
$interval['from'] = '2017-01-02 00:00:00';
$interval['to'] = '2017-02-06 23:59:59';
$start = Carbon::createFromFormat('Y-m-d H:i:s', $interval['from'])->startOfMonth();
$end = Carbon::createFromFormat('Y-m-d H:i:s', $interval['to'])->startOfMonth()->addMonth();
$separate = CarbonInterval::month();
$period = new \DatePeriod($start, $separate, $end);
foreach ($period as $dt) {
dump($dt);
}
But as result I'm getting:
Carbon\Carbon(3) {
date => "2017-01-01 00:00:00.000000" (26)
timezone_type => 3
timezone => "Europe/Prague" (13)
}
Carbon\Carbon(3) {
date => "2017-02-01 00:00:00.000000" (26)
timezone_type => 3
timezone => "Europe/Prague" (13)
}
It is grouped by month but I need to get whole month period, I mean from
2017-01-02 00:00:00 to 2017-01-31 23:59:59
2017-02-01 00:00:00 to 2017-02-06 23:59:59.
Output:
$array = [
0 => [
'from' => '2017-01-02 00:00:00',
'to' => '2017-01-31 23:59:59'
],
1 => [
'from' => '2017-02-01 00:00:00',
'to' => '2017-02-06 23:59:59'
]
];
What is the easiest way to achive it?
Edit: Here is a little bit modified Carbon version of accepted answer, maybe somebody will need it:
$interval['from'] = '2017-01-02 00:00:00';
$interval['to'] = '2017-04-08 23:59:59';
$interval_from = Carbon::createFromFormat('Y-m-d H:i:s', $interval['from']);
$interval_to = Carbon::createFromFormat('Y-m-d H:i:s', $interval['to']);
$result = [];
foreach (range($interval_from->month, $interval_to->month) as $x) {
$to = $interval_from->copy()->endOfMonth();
if ($x == $interval_to->month) {
$result[] = ["from" => $interval_from, "to" => $interval_to];
} else {
$result[] = ["from" => $interval_from, "to" => $to];
}
$interval_from = $to->copy()->addSecond();
}
PHP code demo
Try this solution you can change from one month to another month(not year) and then check. Lengthy solution but hopefully works correctly, putting explaination.
<?php
ini_set('display_errors', 1);
$from=$interval['from'] = '2017-01-02 00:00:00';
$interval['to'] = '2017-03-07 23:59:59';
$month1=date("m", strtotime($interval['from']));
$month2=date("m", strtotime($interval['to']));
$result=array();
foreach(range($month1, $month2) as $x)
{
$dateTimeObj= new DateTime($from);
$dayDifference=($dateTimeObj->format('d')-1);
$dateTimeObj= new DateTime($from);
$dateTimeObj->add(new DateInterval("P1M"));
$dateTimeObj->sub(new DateInterval("P".$dayDifference."DT1S"));
$to= $dateTimeObj->format("Y-m-d H:i:s");
if($x==$month2)
{
$dateTimeObj= new DateTime($interval['to']);
$noOfDays=$dateTimeObj->format("d");
$dateTimeObj->sub(new DateInterval("P".($noOfDays-1)."D"));
$from=$dateTimeObj->format("Y-m-d H:i:s");
$result[]=array("from"=>$from,"to"=>$interval['to']);
}
else
{
$result[]=array("from"=>$from,"to"=>$to);
}
//adding 1 second to $to for next time to be treated as $from
$dateTimeObj= new DateTime($to);
$dateTimeObj->add(new DateInterval("PT1S"));
$from= $dateTimeObj->format("Y-m-d H:i:s");
}
print_r($result);

How to get all months between two dates in PHP?

I have 2 dates. I want to get all months with total days in each.
How can I do this in PHP?
For example
$date1 = '2013-11-13'; // yy-mm-dd format
$date2 = '2014-02-14';
Output
Months Total Days
-----------------------
11-2013 30
12-2013 31
01-2014 31
02-2014 28
Just try with:
$date1 = '2013-11-15';
$date2 = '2014-02-15';
$output = [];
$time = strtotime($date1);
$last = date('m-Y', strtotime($date2));
do {
$month = date('m-Y', $time);
$total = date('t', $time);
$output[] = [
'month' => $month,
'total' => $total,
];
$time = strtotime('+1 month', $time);
} while ($month != $last);
var_dump($output);
Output:
array (size=4)
0 =>
array (size=2)
'month' => string '11-2013' (length=7)
'total' => string '30' (length=2)
1 =>
array (size=2)
'month' => string '12-2013' (length=7)
'total' => string '31' (length=2)
2 =>
array (size=2)
'month' => string '01-2014' (length=7)
'total' => string '31' (length=2)
3 =>
array (size=2)
'month' => string '02-2014' (length=7)
'total' => string '28' (length=2)
Try the below given code :
$date1 = '2013-11-15'; // yy-mm-dd format
$date2 = '2014-02-15';
$start = new DateTime($date1);
$start->modify('first day of this month');
$end = new DateTime($date2);
$end->modify('first day of next month');
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt) {
echo $dt->format("Y-m") ." " ;
echo cal_days_in_month(CAL_GREGORIAN,$dt->format("m"),$dt->format("Y")) . "<br/>";
}
Also, checkout this link for more
i used timestamp and date:
$date1 = '2013-11-15'; // yy-mm-dd format
$date2 = '2014-02-15';
$d1 = strtotime('2013-11-15');
$d2 = strtotime('2014-02-15');
while ($d1 <= $d2) {
echo date('m-d-Y', $d1)." | ";
echo cal_days_in_month(CAL_GREGORIAN, date('m', $d1), date('Y', $d1)) ."<br>";
$d1 = strtotime("+1 month", $d1);
}
This should help you, Check out,
$date1 = new DateTime('2013-11-15');
$date1->modify('first day of this month');
$date2 = new DateTime('2014-02-15');
$date2->modify('first day of next month');
$interval = DateInterval::createFromDateString('1 month');
$value = new DatePeriod($date1, $interval, $date2);
foreach ($value as $dates) {
echo $dates->format("m- Y")."-->".cal_days_in_month(0,$dates->format("m"),$dates->format("Y"))."<br>\n";
}
This is what i came up with:
$arr_months = array();
$date1 = new DateTime('2013-11-15');
$date2 = new DateTime('2014-02-15');
$month1 = new DateTime($date1->format('Y-m')); //The first day of the month of date 1
while ($month1 < $date2) { //Check if the first day of the next month is still before date 2
$arr_months[$month1->format('Y-m')] = cal_days_in_month(CAL_GREGORIAN, $month1->format('m'), $month1->format('Y')); //Add it to the array
$month1->modify('+1 month'); //Add one month and repeat
}
print_r($arr_months);
It creates an associative array with the month as key and the number of days as value.
The array created from the example would be:
Array
(
[2013-11] => 30
[2013-12] => 31
[2014-01] => 31
[2014-02] => 28
)
With a foreach loop you will be able to scroll trough the array easily.
You can use the DateTime class along with cal_day_in_month() like this
$datetime1 = "2014-02-15";
$datetime2= "2013-03-15";
$date1 = new DateTime($datetime1);
$date2 = new DateTime($datetime2);
while (date_format($date2, 'Y-m') <= date_format($date1, 'Y-m'))
{
$date2 = $date2->add(new DateInterval('P1M'));
echo $date2->format('Y-m')." | ".cal_days_in_month(CAL_GREGORIAN, $date2->format('m'), $date2->format('Y'))."<br>";
}

php function for get all mondays within date range

Example:
$startDate is Monday 2007-02-05 and $endDate is Tuesday 2007-02-20. Then I want it to list:
Monday 2007-02-05
Monday 2007-02-12
Monday 2007-02-19
I looked at the PHP manual and found this to get all the days between two dates. But how to do it the way i want? PHP Code:
Rather than get all days and loop through them all, get the first Monday after the start date and then iterate 7 days at a time:
$endDate = strtotime($endDate);
for($i = strtotime('Monday', strtotime($startDate)); $i <= $endDate; $i = strtotime('+1 week', $i))
echo date('l Y-m-d', $i);
I needed the same and created a simple method.
public function getMondaysInRange($dateFromString, $dateToString)
{
$dateFrom = new \DateTime($dateFromString);
$dateTo = new \DateTime($dateToString);
$dates = [];
if ($dateFrom > $dateTo) {
return $dates;
}
if (1 != $dateFrom->format('N')) {
$dateFrom->modify('next monday');
}
while ($dateFrom <= $dateTo) {
$dates[] = $dateFrom->format('Y-m-d');
$dateFrom->modify('+1 week');
}
return $dates;
}
Then use it.
$dateFromString = '2007-02-05';
$dateToString = '2007-02-20';
var_dump($this->getMondaysInRange($dateFromString, $dateToString));
Result:
array (size=3)
0 => string '2007-02-05' (length=10)
1 => string '2007-02-12' (length=10)
2 => string '2007-02-19' (length=10)
Maybe it will be helpful for somebody.
You can use below function to get a array of dates between a date range of specific day.
You have to input start date, end date and day number in number.The day number is as follow.
1 = Monday, 2 = Tuesday, 3 = Wednesday, 4 = Thursday. 5 = Friday, 6 = Saturday, 7 = Sunday.
function getDateForSpecificDayBetweenDates($startDate,$endDate,$day_number){
$endDate = strtotime($endDate);
$days=array('1'=>'Monday','2' => 'Tuesday','3' => 'Wednesday','4'=>'Thursday','5' =>'Friday','6' => 'Saturday','7'=>'Sunday');
for($i = strtotime($days[$day_number], strtotime($startDate)); $i <= $endDate; $i = strtotime('+1 week', $i))
$date_array[]=date('Y-m-d',$i);
return $date_array;
}
for ($i = strtotime($startDate); $i <= strtotime($endDate); $i = strtotime('+1 day', $i)) {
if (date('N', $i) == 1) //Monday == 1
echo date('l Y-m-d', $i); //prints the date only if it's a Monday
}
i Create A class, You get All Days In range Date Group By Name of Day:
class DayHelper{
const MONDAY = 'Mon';
const TUESDAY = 'Tue';
const WEDENSDAY = 'Wed';
const THURSDAY = 'Thu';
const FRIDAY = 'Fri';
const SATURDAY = 'Sat';
const SUNDAY = 'Sun';
public function GetYeardays($dateStart, $dateend){
$period = new \DatePeriod(
new \DateTime($dateStart), new \DateInterval('P1D'), (new \DateTime($dateend))
);
$dates = iterator_to_array($period);
$arrayreturn = array();
foreach ($dates as $val) {
$date = $val->format('Y-m-d'); //format date
$get_name = date('l', strtotime($date)); //get week day
$day_name = substr($get_name, 0, 3); // Trim day name to 3 chars
switch ($day_name) {
case self::MONDAY:
$MONDAY[] = $date;
$arrayreturn[self::MONDAY] = $MONDAY;
break;
case self::TUESDAY:
$TUESDAY[] = $date;
$arrayreturn[self::TUESDAY] = $TUESDAY;
break;
case self::WEDENSDAY:
$WEDENSDAY[] = $date;
$arrayreturn[self::WEDENSDAY] = $WEDENSDAY;
break;
case self::THURSDAY:
$THURSDAY[] = $date;
$arrayreturn[self::THURSDAY] = $THURSDAY;
break;
case self::FRIDAY:
$FRIDAY[] = $date;
$arrayreturn[self::FRIDAY] = $FRIDAY;
break;
case self::SATURDAY:
$SATURDAY[] = $date;
$arrayreturn[self::SATURDAY] = $SATURDAY;
break;
case self::SUNDAY:
$SUNDAY[] = $date;
$arrayreturn[self::SUNDAY] = $SUNDAY;
break;
}
}
return $arrayreturn;
}
}
The Output will be like this
array (size=7)
'Fri' =>
array (size=5)
0 => string '2016/01/01' (length=10)
1 => string '2016/01/08' (length=10)
2 => string '2016/01/15' (length=10)
3 => string '2016/01/22' (length=10)
4 => string '2016/01/29' (length=10)
'Sat' =>
array (size=5)
0 => string '2016/01/02' (length=10)
1 => string '2016/01/09' (length=10)
2 => string '2016/01/16' (length=10)
3 => string '2016/01/23' (length=10)
4 => string '2016/01/30' (length=10)
'Sun' =>
array (size=4)
0 => string '2016/01/03' (length=10)
1 => string '2016/01/10' (length=10)
2 => string '2016/01/17' (length=10)
3 => string '2016/01/24' (length=10)
'Mon' =>
array (size=4)
0 => string '2016/01/04' (length=10)
1 => string '2016/01/11' (length=10)
2 => string '2016/01/18' (length=10)
3 => string '2016/01/25' (length=10)
'Tue' =>
array (size=4)
0 => string '2016/01/05' (length=10)
1 => string '2016/01/12' (length=10)
2 => string '2016/01/19' (length=10)
3 => string '2016/01/26' (length=10)
'Wed' =>
array (size=4)
0 => string '2016/01/06' (length=10)
1 => string '2016/01/13' (length=10)
2 => string '2016/01/20' (length=10)
3 => string '2016/01/27' (length=10)
'Thu' =>
array (size=4)
0 => string '2016/01/07' (length=10)
1 => string '2016/01/14' (length=10)
2 => string '2016/01/21' (length=10)
3 => string '2016/01/28' (length=10)
I made some changes to response https://stackoverflow.com/a/37300272/6871295
Then I can get the days between dates for any day and return format.
public function getWeekDayInRange($weekday, $dateFromString, $dateToString, $format = 'Y-m-d')
{
$dateFrom = new \DateTime($dateFromString);
$dateTo = new \DateTime($dateToString);
$dates = [];
if ($dateFrom > $dateTo) {
return $dates;
}
if (date('N', strtotime($weekday)) != $dateFrom->format('N')) {
$dateFrom->modify("next $weekday");
}
while ($dateFrom <= $dateTo) {
$dates[] = $dateFrom->format($format);
$dateFrom->modify('+1 week');
}
return $dates;
}
This is code for fetching the weekday of "$startdate" and counting the number of weekdays between two dates.
`$startdate` = '2015-03-01';
`$endate` = '2015-03-31';
`$recurringDay` = date('N', strtotime($startdate)); // recurring Day from date i.e monday = 1, Tuesday = 2 ...etc
$begin = new DateTime(`$startdate`);
$end = new DateTime(date('Y-m-d',strtotime('+1 day', strtotime($endate))));
while($begin format('Y-m-d');
$day[] = $begin->format('N');
$begin->modify('+1 day');
}
$c=0; // counter starts
foreach($day as $key=>$dt) {
if ($dt==`$recurringDay`) // compare it
{
$k[] = $key;
$c++;
}
}
`$nofDays` = $c; // number of mondays , tuesday
foreach($k as $pp) {
//adding session code
`$recurringDatetime[]` = $period[$pp]; // recurring dates
}
print_r(`$recurringDatetime`); // array of dates of monday, tuesday ..etc
$dates = array();
$dates[] = strtotime($start);
for($i = 0; $i <= 12; $i++){
$dates[] = strtotime('+1 week', $dates[$i]);
}
foreach($dates as $date){ echo date("d.m.Y", $date); }
I had similar issue and courses can start on any day. This script picks starting day and collect next days every week until the wanted amount (12 in this case).
Convert $startDate and $endDate before that to timestamps:
foreach ($date = $startDate; $date <= $endDate; $date += 60 * 60 * 24) {
if (strftime('%w', $date) == 1) {
$mondays[] = strftime('%A %Y-%m-%d', $date);
}
}
simply you can add as,
$date_from = "2007-02-05";
$date_from = strtotime($date_from);
$date_to="2007-02-20";
$date_to = strtotime($date_to);
for ($i=$date_from; $i<=$date_to; $i+=86400) {
$day = date("Y-m-d", $i);
$unixTimestamp = strtotime($day);
$dayOfWeek = date("l", $unixTimestamp);
if ($dayOfWeek == "Monday") {
echo $day ."is a". $dayOfWeek;
}
}//end for

Categories