Validate Date time through laravel Validation

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Validate Date Time with Laravel Validation - An In-Depth Guide Introduction: Laravel is a versatile and powerful PHP framework that makes building web applications effortless. One of its key features is laravel validation, which helps developers ensure that user input meets specific criteria before processing or storing it in the database. In this blog post, we will focus on validating date time inputs using Laravel's built-in validation rules. A Brief Overview: Laravel validation provides a convenient way to validate form data and user input. It comes with various predefined rules for different types of inputs, such as email addresses, URLs, file sizes, and so on. However, it lacks specific rules for date and time validation. To compensate for this shortcoming, we can use custom validation logic or mix the Laravel default rules to create more comprehensive validation. Custom Validation: If you want complete control over your date-time input validation, you can write custom validation rules that test whether the input follows specific format and restrictions. Here's an example of a custom validation rule for checking the time between 7 AM and 10 PM (19:00):
public function validateTime($attribute, $value, $fail) {
    // Format the user-entered date/time as '2020-12-23 17:40:00'
    $currentDateTime = Carbon::createFromFormat('Y-m-d H:i:s', $value);
    if (!Carbon::now()->between($currentDateTime->copy(new DateTimeZone('Asia/Singapore'))->startOfDay(), $currentDateTime->copy(new DateTimeZone('Asia/Singapore'))->endOfDay())) {
        $fail("Time must be between 7 AM and 10 PM");
    }
}
In this code, we use Carbon to format the user-entered date time and create two new instances: one at the start of the day and another at the end. We then check if the user's input falls between these two dates. If it doesn't fall within the specified range, the validation fails with a custom error message. Using Laravel's Built-in Features: Laravel has some built-in rules that can be used for validating date times. For instance, you can use 'date_format:' to check if the input matches a specific format like 'Y-m-d H:i'. You may also utilize the 'before' or 'after' validation rules to ensure that the date is not in the past or future. To add time restrictions and further control the validations, you can combine these rules with Laravel's custom rules.
public function validateDateTimes($request, $validator) {
    $validator->extend('isDateTimeAfterNow', function ($attribute, $value, $parameters, $validator) {
        // Format the user-entered date/time as '2020-12-23 17:40:00'
        $currentDateTime = Carbon::createFromFormat('Y-m-d H:i:s', $value);
        if (!Carbon::now()->between($currentDateTime->copy(new DateTimeZone('Asia/Singapore'))->startOfDay(), $currentDateTime->copy(new DateTimeZone('Asia/Singapore'))->endOfDay())) {
            return false;
        }
        return true;
    });
    // Custom rule to check if the date time is greater than today's time
    $validator->rule('isDateTimeAfterNow', 'required|date_format:Y-m-d H:i|after_or_equal:' . Carbon::now()->format('Y-m-d H:i:s'));
}
In this code, we create a custom validation rule called 'isDateTimeAfterNow' that ensures the user input for date time is greater than or equal to the current time. We use Laravel's date_format and after_or_equal rules in conjunction with our custom logic to achieve the desired validation result. Conclusion: Laravel's built-in validation features offer a powerful solution for validating user inputs, including date times. While custom validation can provide more granular control over your application's input validation process, it requires additional coding effort and attention to detail. By combining Laravel's default rules with custom logic, you can create robust validation rules that cater to the specific needs of your web application. Remember always to use clear and informative error messages to guide users through the form submission process.