php-fakerHow can I generate a fake date in the future with PHP Faker?
You can generate a fake date in the future with PHP Faker using the dateTimeBetween()
method. This method takes two parameters, the first being the start date and the second being the end date. Here is an example of generating a future date with PHP Faker:
<?php
require_once 'vendor/autoload.php';
$faker = Faker\Factory::create();
$futureDate = $faker->dateTimeBetween('now', '+5 days');
echo $futureDate->format('Y-m-d H:i:s');
// Output: 2020-11-19 11:14:25
?>
The code above uses the dateTimeBetween()
method to generate a future date between the current date and 5 days from now. The dateTimeBetween()
method returns a DateTime
object, so we must use the format()
method to convert it to the desired format. In this case, we used the format Y-m-d H:i:s
to output a date string in the format 2020-11-19 11:14:25
.
Helpful links
More of Php Faker
- How can I generate a fake timestamp using PHP Faker?
- How do I use the Laravel Faker numerify function?
- How do I generate JSON data using PHP Faker?
- How do I generate a zip file using PHP Faker?
- How do I generate a fake year in Laravel using Faker?
- How do I generate a valid VAT number using Laravel Faker?
- How can I generate a zip code using Laravel Faker?
- How can I generate fake data in XLSX format using PHP Faker?
- How do I check which version of Laravel Faker I am using?
- How do I use PHP Faker to generate XML data?
See more codes...