DatePeriod
new DatePeriod(DateTimeInterface $start, DateInterval $interval, DateTimeInterface $end, int $options = 0)
A date-range iterator: walks a series of dates given a start, an interval and an exclusive end, usable directly in foreach.
C C23
#include <stdio.h>
#include <string.h>
#include <time.h>
int main(void) {
/* C has no period iterator: struct tm +1 day at a time, timegm normalizes the fields (Feb 29 rolling over the month) */
struct tm t;
memset(&t, 0, sizeof t);
t.tm_year = 2024 - 1900;
t.tm_mon = 1; /* 0-based: February */
t.tm_mday = 27;
struct tm end;
memset(&end, 0, sizeof end);
end.tm_year = 2024 - 1900;
end.tm_mon = 2; /* March */
end.tm_mday = 2;
while (timegm(&t) < timegm(&end)) {
char buf[16];
strftime(buf, sizeof buf, "%Y-%m-%d", &t);
printf("%s\n", buf);
t.tm_mday += 1;
timegm(&t); /* Out-of-range fields (e.g. 2/30) normalize into the next month */
}
return 0;
}
2024-02-27 2024-02-28 2024-02-29 2024-03-01
C++ C++26
#include <cstdio>
#include <cstring>
#include <ctime>
int main() {
// C++ has no period iterator: same as C — struct tm +1 day at a time, timegm normalizes the fields
std::tm t{};
t.tm_year = 2024 - 1900;
t.tm_mon = 1; // 0-based: February
t.tm_mday = 27;
std::tm end{};
end.tm_year = 2024 - 1900;
end.tm_mon = 2; // March
end.tm_mday = 2;
while (timegm(&t) < timegm(&end)) {
char buf[16];
std::strftime(buf, sizeof buf, "%Y-%m-%d", &t);
std::printf("%s\n", buf);
t.tm_mday += 1;
timegm(&t); // Out-of-range fields (e.g. 2/30) normalize into the next month
}
return 0;
}
2024-02-27 2024-02-28 2024-02-29 2024-03-01
C# v11.0.100
using System.Globalization;
// No built-in period iterator: hand-written loop; DateTime.AddDays is calendar-aware (Feb 29 correct automatically)
var start = new DateTime(2024, 2, 27);
var end = new DateTime(2024, 3, 2);
for (var d = start; d < end; d = d.AddDays(1)) {
Console.WriteLine(d.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
}
2024-02-27 2024-02-28 2024-02-29 2024-03-01
Go v1.27.4
package main
import (
"fmt"
"time"
)
func main() {
// Go has no period iterator: hand-written loop; AddDate is calendar-aware (Feb 29 correct automatically)
start := time.Date(2024, 2, 27, 0, 0, 0, 0, time.UTC)
end := time.Date(2024, 3, 2, 0, 0, 0, 0, time.UTC)
for d := start; d.Before(end); d = d.AddDate(0, 0, 1) {
fmt.Println(d.Format("2006-01-02"))
}
}
2024-02-27 2024-02-28 2024-02-29 2024-03-01
Java v27.0.1
import java.time.LocalDate;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
// Stream.iterate: the functional take on date-range iteration (takeWhile cuts off before the endpoint, exclusive)
LocalDate start = LocalDate.of(2024, 2, 27);
LocalDate end = LocalDate.of(2024, 3, 2);
Stream.iterate(start, d -> d.plusDays(1))
.takeWhile(d -> d.isBefore(end))
.map(LocalDate::toString)
.forEach(System.out::println);
}
}
2024-02-27 2024-02-28 2024-02-29 2024-03-01
JavaScript v26.13.1
// Date has no range iteration: hand-written millisecond stepping loop (UTC basis avoids DST/timezone shifts)
const start = Date.UTC(2024, 1, 27), end = Date.UTC(2024, 2, 2);
for (let t = start; t < end; t += 86400000) {
console.log(new Date(t).toISOString().slice(0, 10));
}
2024-02-27 2024-02-28 2024-02-29 2024-03-01
PHP v8.6.0
date_default_timezone_set("UTC");
// DatePeriod: a date iterator from start/interval/end (endpoint exclusive; feed it straight to foreach)
$period = new DatePeriod(
new DateTime("2024-02-27"),
new DateInterval("P1D"),
new DateTime("2024-03-02")
);
foreach ($period as $d) {
echo $d->format("Y-m-d"), "\n";
}
2024-02-27 2024-02-28 2024-02-29 2024-03-01
Python v3.15.2
import datetime
# Python has no built-in date-range iterator: a hand-written generator is the idiom
def date_range(start, end):
d = start
while d < end: # Exclusive endpoint, same semantics as PHP's DatePeriod
yield d
d += datetime.timedelta(days=1)
for d in date_range(datetime.date(2024, 2, 27), datetime.date(2024, 3, 2)):
print(d.isoformat())
2024-02-27 2024-02-28 2024-02-29 2024-03-01
Rust v1.99.1
// chrono has no range iterator: hand-written while loop (exclusive endpoint)
fn main() {
let mut d = chrono::NaiveDate::from_ymd_opt(2024, 2, 27).unwrap();
let end = chrono::NaiveDate::from_ymd_opt(2024, 3, 2).unwrap();
while d < end {
println!("{}", d.format("%Y-%m-%d"));
d += chrono::Duration::days(1);
}
}
2024-02-27 2024-02-28 2024-02-29 2024-03-01
TypeScript v26.13.1
// Date has no range iteration: hand-written millisecond stepping loop (UTC basis avoids DST/timezone shifts)
const start = Date.UTC(2024, 1, 27), end = Date.UTC(2024, 2, 2);
for (let t: number = start; t < end; t += 86400000) {
console.log(new Date(t).toISOString().slice(0, 10));
}
2024-02-27 2024-02-28 2024-02-29 2024-03-01
Language differences
C: no range iteration — increment the struct tm day field and let timegm normalize (2/30 carries into 3/1 automatically); Feb 29 exists in leap years, no special-casing needed.
C++: same as C; C++20 would allow std::chrono calendar types (this toolchain is C++17).
C#: a loop plus AddDays; Enumerable.Range(0, n) projection works too, no essential difference.
Go: a loop plus AddDate(0, 0, 1); AddDate is calendar-aware and normalizes across months automatically.
Java: Stream.iterate(seed, next) with takeWhile is the closest functional iterator style; d.isBefore(end) expresses the exclusive end.
JavaScript: fixed 86400000 ms steps are only safe in UTC terms (local time skews an hour across DST days); the Temporal proposal will eventually provide built-in range iteration.
PHP: DatePeriod(start, interval, end) excludes the end (2024-03-02 absent above); the third argument may also be an int recurrence count (start + interval + recurrences); the DatePeriod::EXCLUDE_START_DATE option drops the first date; it implements Traversable so foreach works directly, iterator_to_array materializes it.
Python: a generator is the idiomatic hand-written form; the third-party pandas.date_range offers the full feature set (frequency aliases and closed/open bounds).
Rust: while plus NaiveDate += Duration::days(1); itertools or third-party chrono iterator crates can wrap it into an iterator.
TypeScript: same as JavaScript.