7

I'm writing validator rules for checking if the data is valid before adding a new Eloquent model record.

It's quite clear with strings, decimals and integers.

But what about a timestamp field?

I added the column using timestamp method in one of my DB migrations, it works just fine. The only thing I need is to make sure the passed value will be a valid timestamp before executing the query.

Is there a direct/simple way or should I write a regexp for that?

Thank you

1
  • If there is one, I haven't seen it. I use a regex.
    – Joel Hinz
    May 18, 2015 at 11:13

5 Answers 5

9

I think that validate like this..

$rules = ['start_at' => 'date_format:Y-m-d H:i:s'];

Let's test this rule

public function testSimpleTimeStampValidation()
{

    $data = ['start_at'  => '2015-12-1 12:12:58'];


    $rules = ['start_at' => 'date_format:Y-m-d H:i:s'];

    $validator = \Validator::make($data, $rules);

    $this->assertTrue($validator->passes()); // passed !        
}

Again trying test with unwanted date format..

public function testSimpleTimeStampValidation()
{

    $data = ['start_at'  => '12-1-2015 12:12:58'];


    $rules = ['start_at' => 'date_format:Y-m-d H:i:s'];

    $validator = \Validator::make($data, $rules);

    $this->assertTrue($validator->passes()); // Failed !        
}

It looks this rules works very well..

more details Data Format Validation in L5

2
  • this might work. I have to return to this issue some time and see what I've done almost 2 months ago. Thanks
    – MaGnetas
    Jul 8, 2015 at 10:26
  • I have tested ın PHPUnit Test. I sure that "date_format:Y-m-d H:i:s" rules works too
    – MURATSPLAT
    Jul 8, 2015 at 15:31
5

Use this rule

   public function rules()
    {
        return [
            'date_time' => 'date_format:Y-m-d H:i:s'
        ];
    }
0

You can attempt to use Carbon to parse the timestamp for you then run your validation in nice human-readable methods:

$date = Carbon::createFromTimeStamp( (int) $timestamp );
$min = Carbon::create( 2014, 12, 31, 23, 59, 59 );
$max = Carbon::create( 2015, 12, 31, 23, 59, 59 );
return $date->gt( $min ) && $date->lte( $max );

For actually validating the raw timestamp itself though, there's not much I can come up with other than just doing the same thing as above with raw numbers:

return 1420030799 < $timestamp && $timestamp <= 1451566799;

but that's much more difficult to read and maintain than the Carbon method.

-1

There is no validation rule for timestamps in laravel. If you need to validate timestamps you can use somthing like that:

function isValidTimeStamp($timestamp)
{
    return ((string) (int) $timestamp === $timestamp) 
        && ($timestamp <= PHP_INT_MAX)
        && ($timestamp >= ~PHP_INT_MAX);
}

If you generate the timestamp in php there is no need to validate it. If it is a user input you could use the date validation rule of the laravel validator before you convert it into a timestamp.

-1

Just create a new validation rule in laravel to validate the timestamp...

Validator::extend('isTimeStamp', function($attribute, $value, $parameters)
{
    return ((string) (int) $value === $value) 
           && ($value <= PHP_INT_MAX)
           && ($value >= ~PHP_INT_MAX);
});

You can now use isTimeStamp validation rule to validate timestamp.

2
  • What does (string) (int) do before $value? Mar 5, 2020 at 15:53
  • @IvankaTodorova $value is considered a string, so first, you cast $value as an int (so "123" will become integer 123, but "a_string" will become integer 0). Then you cast again the previously casted value as a string again (so int 123 becomes "123"), and you compare this to the initial value, only a real int will still contain the initial value. Note that I would also cast (string)$value in this case. This is sourced from stackoverflow.com/questions/2524680/…
    – Mtxz
    Jul 21, 2021 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.