[GH-ISSUE #1881] DatetimeImmutable #802

Closed
opened 2026-03-04 02:18:10 +03:00 by kerem · 1 comment
Owner

Originally created by @Misosooup on GitHub (Apr 10, 2024).
Original GitHub issue: https://github.com/Seldaek/monolog/issues/1881

Monolog version 1|2|3?

Write your question here.

Hi,

In the Logger.php, there is a function called addRecord which takes in DateTimeImmutable. This object is an object of the Monolog package.

In that function, you have stated that this argument is log date to log in the past or the future. However, when creating the Monolog DateTimeImmutable object, there is no way to set the date for this object. It always default to now. There is no way for me to pass through the base DateTimeImmutable object to the addRecord function as it is strict typed to Monolog\DateTimeImmutable.

    public function __construct(bool $useMicroseconds, ?DateTimeZone $timezone = null)
    {
        $this->useMicroseconds = $useMicroseconds;

        parent::__construct('now', $timezone);
    }
public function addRecord(int $level, string $message, array $context = [], DateTimeImmutable $datetime = null): bool

I feel like there is some inconsistency here as the function allows a future or a past date, yet the DateTimeImmutable object itself doesn't. Are we meant to use it like this?

Originally created by @Misosooup on GitHub (Apr 10, 2024). Original GitHub issue: https://github.com/Seldaek/monolog/issues/1881 Monolog version 1|2|3? Write your question here. Hi, In the Logger.php, there is a function called `addRecord` which takes in DateTimeImmutable. This object is an object of the Monolog package. In that function, you have stated that this argument is log date to log in the past or the future. However, when creating the Monolog DateTimeImmutable object, there is no way to set the date for this object. It always default to `now`. There is no way for me to pass through the base DateTimeImmutable object to the addRecord function as it is strict typed to Monolog\DateTimeImmutable. ``` public function __construct(bool $useMicroseconds, ?DateTimeZone $timezone = null) { $this->useMicroseconds = $useMicroseconds; parent::__construct('now', $timezone); } ``` ``` public function addRecord(int $level, string $message, array $context = [], DateTimeImmutable $datetime = null): bool ``` I feel like there is some inconsistency here as the function allows a future or a past date, yet the DateTimeImmutable object itself doesn't. Are we meant to use it like this?
kerem 2026-03-04 02:18:10 +03:00
  • closed this issue
  • added the
    Support
    label
Author
Owner

@Seldaek commented on GitHub (Apr 12, 2024):

The way to do it is (new DateTimeImmutable(true))->modify('2022-03-04 05:06:07') if you want to set the date to something specific.

<!-- gh-comment-id:2051375637 --> @Seldaek commented on GitHub (Apr 12, 2024): The way to do it is `(new DateTimeImmutable(true))->modify('2022-03-04 05:06:07')` if you want to set the date to something specific.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/monolog#802
No description provided.