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

MaGnetas
  • 4,918
  • 4
  • 32
  • 52

5 Answers5

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

MURATSPLAT
  • 4,650
  • 1
  • 13
  • 12
5

Use this rule

   public function rules()
    {
        return [
            'date_time' => 'date_format:Y-m-d H:i:s'
        ];
    }
PHP Worm...
  • 4,109
  • 1
  • 25
  • 48
0

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.

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
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.

Joe
  • 15,669
  • 4
  • 48
  • 83
0

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.

vps
  • 422
  • 2
  • 6
  • What does `(string) (int)` do before `$value`? – Ivanka Todorova Mar 05 '20 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 https://stackoverflow.com/questions/2524680/check-whether-the-string-is-a-unix-timestamp – Mtxz Jul 21 '21 at 14:55