Skip to content

Commit 62ed434

Browse files
committed
[12.x] Fix date_format validation on DST Timezone
fix #56760 Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
1 parent b568cd7 commit 62ed434

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/Illuminate/Validation/Concerns/ValidatesAttributes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ public function validateDateFormat($attribute, $value, $parameters)
625625

626626
foreach ($parameters as $format) {
627627
try {
628-
$date = DateTime::createFromFormat('!'.$format, $value);
628+
$date = DateTime::createFromFormat('!'.$format, $value, new DateTimeZone('UTC'));
629629

630630
if ($date && $date->format($format) == $value) {
631631
return true;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Integration\Validation\Rules;
4+
5+
use Illuminate\Support\Facades\Validator;
6+
use Orchestra\Testbench\TestCase;
7+
use PHPUnit\Framework\Attributes\TestWith;
8+
9+
class DateFormatValidationTest extends TestCase
10+
{
11+
#[TestWith(['UTC'])]
12+
#[TestWith(['Europe/Amsterdam'])]
13+
public function test_it_can_validate_regardless_of_timezone(string $timezone)
14+
{
15+
date_default_timezone_set($timezone);
16+
17+
$payload = ['date' => '2025-03-30 02:00:00'];
18+
$rules = ['date' => 'date_format:Y-m-d H:i:s'];
19+
20+
$validator = Validator::make($payload, $rules);
21+
22+
$this->assertTrue($validator->passes());
23+
$this->assertEmpty($validator->errors()->all());
24+
}
25+
}

0 commit comments

Comments
 (0)