Skip to content

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

Open
dereuromark wants to merge 1 commit into3.nextfrom
add-to-array-method
Open

Add toArray() method to Chronos, ChronosDate, and ChronosTime#506
dereuromark wants to merge 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
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.

1 participant