Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

LTroya's avatar
Level 19

How can I make this format pass date_format request validation?

For some reason my request is parsing any date format to this before starting the validation: 2020-07-10T00:00:00.000000Z

This is my rule: 'entities.*.begin_date' => 'sometimes|date_format:Y-m-d\TH:i:sss\Z but validation fails.

I tried the following formats but not work:

  • Y-m-d\TH:i:sss\Z
  • Y-m-d\TH:i:s\Z

Which format do I have to use in order to pass the validation?

0 likes
17 replies
bugsysha's avatar

From this you can figure out how to format it.

    public function toISOString($keepOffset = false)
    {
        if (!$this->isValid()) {
            return null;
        }

        $yearFormat = $this->year < 0 || $this->year > 9999 ? 'YYYYYY' : 'YYYY';
        $tzFormat = $keepOffset ? 'Z' : '[Z]';
        $date = $keepOffset ? $this : $this->copy()->utc();

        return $date->isoFormat("$yearFormat-MM-DD[T]HH:mm:ss.SSSSSS$tzFormat");
    }
Snapey's avatar

For some reason my request is parsing any date format to this before starting the validation:

How do you know this is the case?

Where does the data come from?

What are you using to inspect the format?

LTroya's avatar
Level 19

Hi @snapey, because I comment the $dates property in my model and doesn't apply the format I mentioned

I have this:

protected $dates = [
        'begin_date',
        'end_date'
    ];
LTroya's avatar
Level 19

Hi @bugsysha, how I can apply that format? Using prepareValidation on request class? Maybe my explanation wasn't enough but I will try that format

I was struggling with php format dates, for example, Y is equal to YYYY on js. I can't build the correct format from the docs

This date 2020-01-01 has the following format on php Y-m-d, js is YYYY-MM-DD

bugsysha's avatar

Just use date_format rule as you show in your first example.

LTroya's avatar
Level 19

@bugsysha that's what I want. But I don't find the correct date format to give it as parameter to date_format

LTroya's avatar
Level 19

@bugsysha

Value: "begin_date" => "2020-01-10T00:00:00.000000Z"

Rule: 'entities.*.begin_date' => 'sometimes|date_format:' . \DateTimeInterface::ATOM,

Validation error message: 0 => "The entities.0.begin_date does not match the format Y-m-d\TH:i:sP."

Maybe it helps, this is the information I get from date_parse_from_format:

array:13 [
  "year" => 2020
  "month" => 1
  "day" => 10
  "hour" => 0
  "minute" => 0
  "second" => 0
  "fraction" => 0.0
  "warning_count" => 0
  "warnings" => []
  "error_count" => 1
  "errors" => array:1 [
    19 => "The timezone could not be found in the database"
  ]
  "is_localtime" => true
  "zone_type" => 0
]

This is the code I use to try to debug formats:

\Validator::extend('date_formats', function ($attribute, $value, $formats) {

            foreach ($formats as $format) {
                $parsed = date_parse_from_format($format, $value);
                if ($parsed['error_count'] === 0 && $parsed['warning_count'] === 0) {
                    return true;
                }
            }
            return false;
        }, 'The :attribute not following the correct formats');
Snapey's avatar

you did not answer my questions, which were designed to help you fix the issue. Your model is not involved in validation, so why mention it?

LTroya's avatar
Level 19

@snapey Sorry, I didn't understand your previous answer, my bad.

The data comes from a test

/** @test */
    function it_should_update_a_debt()
    {
        $this->withoutExceptionHandling();
        $this->signIn();
        $debt = create(Debt::class, ['user_id' => auth()->id()]);
        $debt->name = 'John Doe debt';
        $debt->value = 100;
        $debt->begin_date = '2020-01-10';
        $debt->end_date = '2020-07-10';

        $response = $this->json('POST', '/api/debts', ['entities' => [$debt]]);
        $response->assertStatus(200);

        $this->assertCount(1, auth()->user()->debts);
        $this->assertEquals('John Doe debt', $debt->name);
        $this->assertEquals(100, $debt->value);
        $this->assertEquals('2020-01-10', DateHelper::applyFormat($debt->begin_date));
        $this->assertEquals('2020-07-10', DateHelper::applyFormat($debt->end_date));
    }

Even if I have this lines:

$debt->begin_date = '2020-01-10';
$debt->end_date = '2020-07-10';	

The request class received the following:

"begin_date" => "2020-01-10T00:00:00.000000Z"
 "end_date" => "2020-07-10T00:00:00.000000Z"

If I remove this lines from my model

protected $dates = [
        'begin_date',
        'end_date'
    ];

I received the dates as expected, like this:

 "begin_date" => "2020-01-10"
 "end_date" => "2020-07-10"

That is the reason I mentioned it before.

ktscript's avatar

None of the solutions proposed here have worked for me. For php>7.2, a working option

date_format:Y-m-d\TH:i:s.\0\0\0\Z

Please or to participate in this conversation.