PHP code example of d34dman / consecutive-date

1. Go to this page and download the library: Download d34dman/consecutive-date library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

d34dman / consecutive-date example snippets


use D34dman\ConsecutiveDate\ConsecutiveDate;

// Create a list of DateTime objects
$dateList = [
    new DateTime("2024-03-15"),
    new DateTime("2024-03-14"),
    new DateTime("2024-03-13"),
    new DateTime("2024-03-12"),
    new DateTime("2024-03-10"), // Note the gap here
];

// Count consecutive days backwards from a specific date
$currentDate = new DateTime("2024-03-15");
$consecutiveDays = ConsecutiveDate::countPastDays($currentDate, $dateList, "Y-m-d");
// Result: 4 (March 15, 14, 13, 12)

// Count consecutive days forwards
$startDate = new DateTime("2024-03-12");
$consecutiveDays = ConsecutiveDate::countFutureDays($startDate, $dateList, "Y-m-d");
// Result: 4 (March 12, 13, 14, 15)

use D34dman\ConsecutiveDate\ConsecutiveDateStrings;

// Array of date strings
$dateStrings = [
    "2024-03-15",
    "2024-03-14",
    "2024-03-13",
    "2024-03-12"
];

$currentDate = new DateTime("2024-03-15");
$consecutiveDays = ConsecutiveDateStrings::countPastDays($currentDate, $dateStrings, "Y-m-d");
// Result: 4

// Using different date format
$dateStrings = [
    "15/03/2024",
    "14/03/2024",
    "13/03/2024"
];

$currentDate = new DateTime("15/03/2024");
$consecutiveDays = ConsecutiveDateStrings::countPastDays($currentDate, $dateStrings, "d/m/Y");
// Result: 3