Random date between 2 dates with faker Hey!
I'm using the following code
$date = $request->daterange; // example format: 04.24.2022 - 04.24.2022
$date = str_replace(' ', '', $date);
$date = explode('-', $date);
$faker = \Faker\Factory::create();
$randtime = $faker->dateTimeBetween($date[0], $date[1]);
but when the date range is 04.24.2022 - 04.24.2022 (today) it always insert in the created_at column 2022-04-29 00:00:00 (note the 00:00:00 ). How can I make the faker also generate a time like (14:21:31 ) BUT NOT IN THE FUTURE while generating the date?
$randtime = $faker->dateTimeBetween($date[0], $date[1]." 23:59:59");
@sr57 I want it to generate time only if the user selected today, not set static time. (NOTE: BUT NOT IN THE FUTURE )
Ok, you should do
if ( $date[0] == $date[1] ) $date1=now(); else $date1=$date[1];
$randtime = $faker->dateTimeBetween($date[0], $date1);
@sr57 But it's setting literally the current time like this. That's not what I want.
I'm giving example if TODAY (04.29.2022 - 04.29.2022) is set:
Current time: 17:00:00 (04.29.2022)
Generate time between: 00:00:00 - 17:00:00 (04.29.2022)
@sr57 I'll wait other for other responses.
Your code will set now() if $date[0] == $date[1] is true.
And that's not what I want.
Thanks tho.
Last try
if ( $date[0] == $date[1] && date('m.d.Y',time()) == $date[1] ) $date1=now(); else $date1=$date[1];
$randtime = $faker->dateTimeBetween($date[0], $date1);
You should get the idea of how to dit if it's not exactly what you want.
I still need a solution. @sr57 doesn't understand what I expect but thanks to him tho.
@laralex
I'm back, maybe you can give me/us same samples (inputs,output) , particularly where my code does not fit your need.
and if the range does not end today, it could be any time on the end date?
Your requirements are not really clear.
Why is it a range when all you want is a single date-time?
@Snapey
and if the range does not end today, it could be any time on the end date?
Exactly.
Why is it a range when all you want is a single date-time?
That's how the jQuery plugin works. Even if you select today, it inserts 05.02.2022 - 05.02.2022.
That was what I'm looking for
if($date[1] == date('d.m.Y')) {
$date[1] = $date[1] . " " . date('H:i:s');
}
$faker = \Faker\Factory::create();
$randtime = $faker->dateTimeBetween($date[0], $date[1]);
@sr57 @snapey
Please sign in or create an account to participate in this conversation.