Create a datetime by faker laravel with format dd-mm-yyyy minute hour second, for example : 26/01/2022 20:52:23
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Mastering Custom Datetime Formatting with Laravel Faker: Achieving dd-mm-yyyy HH:MM:SS
As senior developers working within the Laravel ecosystem, we frequently rely on tools like the Faker library to populate our database with realistic test data for seeding factories. While Faker is incredibly powerful for generating random data, sometimes the default output doesn't match the exact presentation format required by our application logic or database schema.
This post addresses a common challenge: how to instruct Laravel Faker to generate a datetime value in a highly specific, localized string format, such as dd-mm-yyyy HH:MM:SS, rather than its default ISO-like standard. We will explore the necessary steps to combine Faker's data generation with PHP's powerful date formatting capabilities to achieve perfect output consistency.
The Challenge with Default Faker Output
When you use methods like $this->faker->dateTime(), Faker generates a native PHP DateTime object or a standardized string representation. By default, this often results in formats like YYYY-MM-DD or full ISO 8601 timestamps.
For example, if you generate a date using the standard method within your factory file:
'from' => $this->faker->dateTime(),
The resulting value might be something like '2023-10-27 14:35:01', which is correct data, but it does not match the required display format of 27-10-2023 14:35:01. To meet specific presentation requirements, we must manually format this output.
The Solution: Combining Faker with PHP Formatting
The solution lies in generating the base date/time object and then using PHP's built-in date() function or the DateTime::format() method to dictate the exact string structure you need. This ensures that regardless of where the data is used—whether seeding a database, displaying it in a view, or logging it—the format is always consistent.
We will use the hyphens and colons specified in your requirement (dd-mm-yyyy HH:MM:SS).
Step-by-Step Implementation
- Generate the DateTime Object: Start by calling the Faker method to get a valid timestamp.
- Use the
format()Method: Apply the desired format string to convert the object into the target string format.
Here is how you would implement this within your Laravel Factory file:
use Illuminate\Support\Str;
use Faker\Factory;
class PostFactory extends Factory
{
protected $faker;
protected function ableToGenerate()
{
$this->faker = Factory::create();
}
public function definition()
{
// Generate the base DateTime object using Faker
$dateTimeObject = $this->faker->dateTimeThisYear();
// Format the output to the desired dd-mm-yyyy HH:MM:SS format
$formattedDateTime = $dateTimeObject->format('d-m-Y H:i:s');
return [
'created_at' => $formattedDateTime, // Store the formatted string
'updated_at' => $formattedDateTime,
];
}
}
Detailed Code Example and Explanation
The critical part is the format string passed to the format() method: 'd-m-Y H:i:s'. Let's break down what these characters mean in PHP's date formatting:
d: Day of the month, 2 digits with leading zero (01 to 31).m: Numeric representation of a month, with leading zeros (01 to 12).Y: A four-digit representation of a year.H: 24-hour format of an hour (00 to 23).i: Minutes with leading zeros (00 to 59).s: Seconds with leading zeros (00 to 59).
By combining these elements with the requested separators (- and : ), we achieve the exact format: dd-mm-yyyy HH:MM:SS.
This practice of injecting custom formatting logic directly into your factory definitions is a fundamental aspect of robust data seeding in Laravel. As you build complex applications, ensuring data integrity and presentation consistency is paramount, which aligns perfectly with the principles discussed on platforms like laravelcompany.com.
Conclusion
Generating specific date formats from Faker requires moving beyond simple method calls and embracing PHP's native string manipulation capabilities. By leveraging $this->faker->dateTime() to obtain a valid object and then applying the precise format('d-m-Y H:i:s') instruction, you gain complete control over the data structure generated by your factories. This approach ensures that your test data is not only random but also perfectly structured for immediate use in your Laravel application.