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

catalyph's avatar

Factory work shift generation

I cannot figure out how to create a datetime field in factory that the endtime datetime entry is within 12 hours of the starttime entry>

I have tried with Carbon and with faker

neither I can get to work properly.

I want to generate a fake date time entry YYYY-MM-DD HH:mm and then based off that I want another datetime entry that is a random number of hours and minutes added and an entry that shows the difference for example

2024-05-20 08:00 - 2024-05-20 15:20 - (9 hours 20 minute)

2024-05-20 20:00 - 2024-05-21 03:45 - (7 hours 45 minute)

to represent workers shift times. My Database has a start_time and end_time column

0 likes
4 replies
chiefguru's avatar

Hi @catalyph you could simply use faker to generate a random number of minutes, between 5 and 11 hours worth then simply add those minutes to the start time to use as your finish time.

maxxd's avatar

If you're looking for totally random dates, you can use the following:

$startTime = Carbon\Carbon::parse(fake()->dateTime());
$endTime = $startTime->clone()->addHours(rand(1,11))->addMinutes(rand(0,59));
1 like

Please or to participate in this conversation.