DatePeriod වර්ගය
DatePeriod වර්ගය නිශ්චිත කාල අන්තරයක් සමඟ ආරම්භක හා අවසාන ලක්ෂ්ය අතර දින අනුක්රමණයක් සාදයි.
පරාමිතීන් ලෙස පිළිගනී: DateTime වස්තුවක් ලෙස ආරම්භක දිනය, DateInterval වස්තුවක් ලෙස කාල අන්තරය, පුනරාවර්තන ගණන හෝ අවසාන දිනය.
ඇඟවුම් කළ පරාසය තුළ ඇති සියලුම දින හරහා පුනරාවර්තනය කිරීමට ඉඩ සලසයි.
වාක්ය රීතිය
new DatePeriod(DateTime $start, DateInterval $interval, int $recurrences);
new DatePeriod(DateTime $start, DateInterval $interval, DateTime $end);
උදාහරණය
1 දිනක කාල අන්තරයක් සහිතව 5 දින කාලපරිච්ඡේදයක් සාදමු:
<?php
$start = new DateTime('2025-01-01');
$interval = new DateInterval('P1D');
$period = new DatePeriod($start, $interval, 4);
foreach ($period as $date) {
echo $date->format('Y-m-d') . '<br>';
}
?>
කේතය ක්රියාත්මක කිරීමේ ප්රතිඵලය:
'2025-01-01'
'2025-01-02'
'2025-01-03'
'2025-01-04'
'2025-01-05'
උදාහරණය
1 සතාහ කාල අන්තරයක් සහිතව දෙදිනක් අතර කාලපරිච්ඡේදයක් සාදමු:
<?php
$start = new DateTime('2025-01-01');
$end = new DateTime('2025-02-01');
$interval = new DateInterval('P1W');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $date) {
echo $date->format('Y-m-d') . '<br>';
}
?>
කේතය ක්රියාත්මක කිරීමේ ප්රතිඵලය:
'2025-01-01'
'2025-01-08'
'2025-01-15'
'2025-01-22'
'2025-01-29'
මෙයද බලන්න
-
DateTimeවර්ගය,
දිනය හා වේලාව සමඟ වැඩ කිරීමට -
DateIntervalවර්ගය,
දින කාල අන්තර සමඟ වැඩ කිරීමට