How to get a dates range (from a JSON array of UNIX dates) instead of single dates one by one?

  date, icalendar, jquery, json, php

I have several JSON files with UNIX dates like so:

[1635724800,1635811200,1635897600,1635984000,1636070400,1636156800,1636243200,1636329600,1636416000,1636502400,1636588800,1636675200,1636761600,1636848000,1636934400,1637020800,1637107200,1637193600,1637280000,1637366400,1637452800,1637539200,1637625600,1637712000,1637798400,1637884800,1637971200,1638057600,1638144000,1638230400,1638316800,1638403200,1638489600,1638576000,1638662400,1638748800,1638835200,1638921600,1639008000,1639094400]

Sometimes the dates are a full month, other times they are an array including only some days of several different months.

I have been able to convert them individually into iCal dates.
But I would need to get a date range whenever possible, to make the output shorter and more practical to read/use.

How could I go through my JSON arrays and generate a date range for each range they cover in reality?

My code so far is this:

<?php

//for each value in JSON give me a single unix date to transform to ICS date

$datadates = file_get_contents("https://www.testurl.com/days?idproperty=".$ID);
$jsondates = json_decode($datadates, true);

//start ical creation...
echo "
BEGIN:VCALENDAR
PRODID:-//RentalWebs//Vacation Rentals Software//EN
VERSION:2.0
CALSCALE:GREGORIAN";

//get blocked days...
foreach($jsondates as $mydata)

    {
         
        echo "BEGIN:VEVENT";
        echo "<br>";

         
        $uStampUTC = $mydata + (1 * 3600);
        $stamp  = date("Ymd", $uStampUTC);

        echo "DTSTAMP:";
        echo $stamp;
        echo "<br>";
        echo "DTSTART:";
        echo $stamp;
        echo "<br>";
        echo "DTEND:";
        echo $stamp+1;
        echo "<br>";
        
        echo "UID:";
        echo $stamp;
        echo "@testurl.com";
        echo "<br>";
        
        echo "END:VEVENT";
         
    }
    
    echo "END:VCALENDAR";
    
?>

Source: Ask PHP

LEAVE A COMMENT