Skip to content

Add toArray() method to Chronos, ChronosDate, and ChronosTime#506

Merged
dereuromark merged 1 commit into3.nextfrom
add-to-array-method
Mar 20, 2026
Merged

Add toArray() method to Chronos, ChronosDate, and ChronosTime#506
dereuromark merged 1 commit into3.nextfrom
add-to-array-method

Conversation

@dereuromark
Copy link
Member

Summary

Adds a toArray() method to return date/time components as an associative array. This is the inverse of the existing createFromArray() method.

  • Chronos::toArray() returns: year, month, day, hour, minute, second, microsecond, timezone
  • ChronosDate::toArray() returns: year, month, day
  • ChronosTime::toArray() returns: hour, minute, second, microsecond

Example Usage

$dt = Chronos::create(2024, 1, 15, 12, 30, 45, 123456, 'America/Toronto');
$array = $dt->toArray();
// [
//     'year' => 2024,
//     'month' => 1,
//     'day' => 15,
//     'hour' => 12,
//     'minute' => 30,
//     'second' => 45,
//     'microsecond' => 123456,
//     'timezone' => 'America/Toronto',
// ]

// Round-trip
$restored = Chronos::createFromArray($array);

Adds a toArray() method to return date/time components as an
associative array. This is the inverse of the existing createFromArray()
method.

- Chronos::toArray() returns year, month, day, hour, minute, second,
  microsecond, and timezone
- ChronosDate::toArray() returns year, month, and day
- ChronosTime::toArray() returns hour, minute, second, and microsecond
@dereuromark dereuromark added this to the 3.next milestone Mar 14, 2026
@dereuromark dereuromark marked this pull request as ready for review March 14, 2026 18:45
@dereuromark dereuromark merged commit 01c9515 into 3.next Mar 20, 2026
8 checks passed
@dereuromark dereuromark deleted the add-to-array-method branch March 20, 2026 11:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants