Reputation: 13
Below is the code which I am using to identify continuous range of dates stored in an array
$lastDate = null;
$currentRange = array();
$holidays=array("2022-12-15","2022-12-16","2022-12-17","2022-12-28","2022-12-29");
foreach($holidays as $holiday)
{
$time_stamp=strtotime($holiday);
if($time_stamp<=$fromdate)
{
if (null === $lastDate)
{
$currentRange[] = $time_stamp;
}
else
{
// get the DateInterval object
$interval = ($time_stamp-$lastDate)/86400;
// // DateInterval has properties for
// // days, weeks. months etc. You should
// // implement some more robust conditions here to
// // make sure all you're not getting false matches
// // for diffs like a month and a day, a year and
// // a day and so on...
if ($interval == 1) {
// add this date to the current range
$currentRange[] = date('m-d-Y',$time_stamp);
} else {
// store the old range and start anew
//$ranges[] = $currentRange;
$currentRange = array(date('m-d-Y',$time_stamp));
}
}
$lastDate = $time_stamp;
}
When I am displaying $currentrange Output is: [1671042600, '12-16-2022', '12-17-2022']
The first timestamp is not getting converted to date time format.
Also the first timestamp returned "1671042600" corresponds to '12-14-2022' while it should be '12-15-2022'.
I am not able to figure out why this weird response, only the first timestamp is not getting correctly converted to date while other timestamps are correctly getting converted to date
Upvotes: 0
Views: 53
Reputation: 1
Change
$time_stamp=strtotime($holiday);
To
$time_stamp = (strtotime($holiday)?strtotime($holiday):$holiday);
Upvotes: 0