验证 Laravel 5 中的时间戳值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30301602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Validate a timestamp value in laravel 5
提问by MaGnetas
I'm writing validator rules for checking if the data is valid before adding a new Eloquent model record.
我正在编写验证器规则,用于在添加新的 Eloquent 模型记录之前检查数据是否有效。
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.
我timestamp
在我的数据库迁移之一中添加了 using方法的列,它工作得很好。我唯一需要的是在执行查询之前确保传递的值是有效的时间戳。
Is there a direct/simple way or should I write a regexp for that?
有没有直接/简单的方法,还是应该为此编写正则表达式?
Thank you
谢谢
回答by MURATSPLAT
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
更多细节L5 中的数据格式验证
回答by PHP Worm...
Use this rule
使用这个规则
public function rules()
{
return [
'date_time' => 'date_format:Y-m-d H:i:s'
];
}
回答by P??
There is no validation rule for timestamps in laravel. If you need to validate timestamps you can use somthing like that:
laravel 中没有时间戳验证规则。如果您需要验证时间戳,您可以使用类似的东西:
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 validatorbefore you convert it into a timestamp.
如果您在 php 中生成时间戳,则无需对其进行验证。如果它是用户输入,您可以在将其转换为时间戳之前使用Laravel 验证器的date
验证规则。
回答by Joe
You can attempt to use Carbonto parse the timestamp for you then run your validation in nice human-readable methods:
您可以尝试使用Carbon为您解析时间戳,然后以良好的人类可读方法运行您的验证:
$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.
但这比 Carbon 方法更难阅读和维护。
回答by vps
Just create a new validation rule in laravel to validate the timestamp...
只需在 laravel 中创建一个新的验证规则来验证时间戳...
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.
您现在可以使用isTimeStamp
验证规则来验证时间戳。