दो तारीखों के बीच की सभी तारीखों को array में प्राप्त करने के तरीके।
मूल फंक्शन (DateTime - अनुशंसित)
<?php
function getDatesBetween(string $startDate, string $endDate, string $format = 'Y-m-d'): array {
$start = new DateTime($startDate);
$end = new DateTime($endDate);
$end->modify('+1 day'); // inclusive के लिए
$dates = [];
$interval = new DateInterval('P1D'); // 1 day
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $date) {
$dates[] = $date->format($format);
}
return $dates;
}
// उपयोग
$dates = getDatesBetween('2024-01-10', '2024-01-15');
print_r($dates);
/*
Array
(
[0] => 2024-01-10
[1] => 2024-01-11
[2] => 2024-01-12
[3] => 2024-01-13
[4] => 2024-01-14
[5] => 2024-01-15
)
*/
Alternative: strtotime() Loop (Legacy)
<?php
function getDatesBetweenLegacy($startDate, $endDate, $format = 'Y-m-d') {
$dates = [];
$current = strtotime($startDate);
$end = strtotime($endDate);
$step = '+1 day';
while ($current <= $end) {
$dates[] = date($format, $current);
$current = strtotime($step, $current);
}
return $dates;
}
Generator Version (Memory Efficient)
<?php
function dateRangeGenerator(string $startDate, string $endDate, string $format = 'Y-m-d'): Generator {
$start = new DateTime($startDate);
$end = new DateTime($endDate);
$end->modify('+1 day');
$interval = new DateInterval('P1D');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $date) {
yield $date->format($format);
}
}
// उपयोग - large ranges के लिए memory efficient
foreach (dateRangeGenerator('2024-01-01', '2024-12-31') as $date) {
// Process one by one
echo $date . "\n";
}
// Array में convert यदि needed
$dates = iterator_to_array(dateRangeGenerator('2024-01-10', '2024-01-15'));
Business Days Only (Weekends Exclude)
<?php
function getBusinessDays(string $startDate, string $endDate, string $format = 'Y-m-d'): array {
$dates = [];
$start = new DateTime($startDate);
$end = new DateTime($endDate);
$end->modify('+1 day');
$interval = new DateInterval('P1D');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $date) {
$dayOfWeek = (int)$date->format('N'); // 1=Mon, 7=Sun
if ($dayOfWeek < 6) { // Mon-Fri only
$dates[] = $date->format($format);
}
}
return $dates;
}
// Holidays exclude करने के लिए
function getBusinessDaysExcludingHolidays(string $start, string $end, array $holidays = []): array {
$holidaySet = array_flip(array_map(fn($d) => (new DateTime($d))->format('Y-m-d'), $holidays));
return array_filter(getBusinessDays($start, $end), function($date) use ($holidaySet) {
return !isset($holidaySet[$date]);
});
}
Custom Intervals
<?php
function getDatesWithInterval(string $start, string $end, string $intervalSpec, string $format = 'Y-m-d'): array {
$dates = [];
$startDt = new DateTime($start);
$endDt = new DateTime($end);
$endDt->modify('+1 day');
$interval = new DateInterval($intervalSpec);
$period = new DatePeriod($startDt, $interval, $endDt);
foreach ($period as $date) {
$dates[] = $date->format($format);
}
return $dates;
}
// Weekly
getDatesWithInterval('2024-01-01', '2024-02-01', 'P1W'); // Every week
// Monthly
getDatesWithInterval('2024-01-01', '2024-12-31', 'P1M'); // Every month
// Every 3 days
getDatesWithInterval('2024-01-01', '2024-01-31', 'P3D');
Carbon Library (Modern PHP Projects)
// composer require nesbot/carbon
use Carbon\CarbonPeriod;
$period = CarbonPeriod::create('2024-01-10', '2024-01-15');
$dates = $period->format('Y-m-d'); // Collection
// Business days
$businessDays = CarbonPeriod::create('2024-01-10', '2024-01-15')
->filter(fn($date) => $date->isWeekday())
->format('Y-m-d');