9951 explained code solutions for 126 technologies


php-fakerHow can I generate a fake date and time in PHP using Faker?


Using the Faker library in PHP, you can generate a fake date and time with a few simple steps.

Here is an example of how to do it:

<?php

require_once 'vendor/autoload.php';

$faker = Faker\Factory::create();

echo $faker->dateTimeThisMonth;

Output example

2020-07-02 13:32:43

The code above consists of the following parts:

  1. require_once 'vendor/autoload.php'; - This loads the Faker library.

  2. $faker = Faker\Factory::create(); - This creates an instance of the Faker library.

  3. echo $faker->dateTimeThisMonth; - This prints out a fake date and time for the current month.

For more information about the Faker library, please refer to the following links:

Edit this code on GitHub