Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

givanov95's avatar

Faker Generating DST Hours

Hello everyone,

I'm working on a Laravel application and using the Faker library to generate timestamps for testing purposes. However, I've encountered a problem related to Daylight Saving Time (DST) transitions.

The issue arises with timestamps that fall into the problematic DST hour. Specifically, I’m seeing errors or unexpected behavior when the Faker library generates times during the 03:00 hour on the day DST starts or ends.

I undertand why it happens, but I cannot automatically get the problematically hour.

When Faker generates timestamps, it sometimes lands on the 03:00 hour, which can be problematic due to the DST transition. This causes issues in my application because the 03:00 hour either does not exist or occurs twice during the DST transition. What I Need Help With

I need a way to automatically identify the problematic hour (like 03:00) so I can handle it appropriately in my code.

Filtering or Adjusting Timestamps:
I’m looking for a method to either filter out or adjust the generated timestamps to avoid the problematic DST hour. Ideally, I want to ensure that all generated timestamps fall into valid hours without DST-related issues. But i would like to make it automatically, not just checking for the hour 3, but get the number 3 somehow.

Here's how I'm using it : 'expected_date_of_delivery_to_buyer' => $this->faker->dateTimeBetween('now', '+2 years'), i also tried to add different timezones as third parameter /UTC, and some more/ but didn`t work

0 likes
2 replies
cosmic_learning's avatar

Nice app idea you've got, a piece of advice for you, make sure you're handling different timezones correctly. You might need to adjust the code based on the specific timezones you're dealing with. Also, be aware that DST rules can change over time, so consider making the code more flexible to handle different DST scenarios.

givanov95's avatar

I created a trait that works now, but i still cannot change the static '03' in the code with dynamic one:

<?php

declare(strict_types=1);

namespace Database\Seeders\Traits;

use DateTime;
use DateTimeZone;

trait FakerDateTimeTrait
{
    /**
     * Generate a datetime avoiding problematic hours like '03:xx:xx'.
     *
     * @param mixed             $startDate
     * @param mixed             $endDate
     * @param null|mixed        $timezone
     * @param null|DateTimeZone $dateTimezone
     */
    public function safeDateTimeBetween($startDate = '-2 years', $endDate = 'now', string $dateTimezone = 'Europe/Sofia')
    {
        $timezone = new DateTimeZone($dateTimezone);

        do {
            $datetime = $this->faker->dateTimeBetween($startDate, $endDate);

            $datetime->setTimezone($timezone);

            $hour = $datetime->format('h');
        } while ('03' == $hour);

        // Return the formatted DateTime object as a string
        return $datetime->format('Y-m-d H:i:s');
    }
}

Please or to participate in this conversation.