Skip to content

Commit d803f77

Browse files
committed
fix: date check error on input < 1970
1 parent f1b1fdd commit d803f77

File tree

2 files changed

+47
-11
lines changed

2 files changed

+47
-11
lines changed

src/Validators.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php declare(strict_types=1);
22
/**
33
* 验证器列表
4+
*
45
* @date 2015.08.04
56
* @note 验证数据; 成功则返回预期的类型, 失败返回 false
67
* @description INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV 几个输入数据常量中的值在请求时即固定下来了,
@@ -1135,9 +1136,9 @@ public static function notIn($val, $dict, $strict = false): bool
11351136
******************************************************************************/
11361137

11371138
/**
1138-
* @param mixed $val
1139+
* @param mixed $val
11391140
* @param string|numeric $start
1140-
* @param bool|mixed $strict
1141+
* @param bool|mixed $strict
11411142
*
11421143
* @return bool
11431144
*/
@@ -1161,9 +1162,9 @@ public static function startWith($val, $start, $strict = true): bool
11611162
}
11621163

11631164
/**
1164-
* @param mixed $val
1165+
* @param mixed $val
11651166
* @param string|numeric $end
1166-
* @param bool|mixed $strict
1167+
* @param bool|mixed $strict
11671168
*
11681169
* @return bool
11691170
*/
@@ -1191,17 +1192,20 @@ public static function endWith($val, $end, $strict = true): bool
11911192
* 校验字段值是否是日期格式
11921193
*
11931194
* @param string|mixed $val 日期
1195+
* @param bool|string $sholdGt0
11941196
*
11951197
* @return boolean
11961198
*/
1197-
public static function date($val): bool
1199+
public static function date($val, $sholdGt0 = false): bool
11981200
{
11991201
if (!$val) {
12001202
return false;
12011203
}
12021204

1203-
// strtotime 转换不对,日期格式显然不对。
1204-
return strtotime((string)$val) > 1;
1205+
// strtotime 转换不对,日期格式显然不对
1206+
$time = strtotime((string)$val);
1207+
1208+
return $sholdGt0 ? $sholdGt0 > 1 : $time !== false;
12051209
}
12061210

12071211
/**
@@ -1225,7 +1229,7 @@ public static function dateEquals($val, $date): bool
12251229
* 校验字段值是否是日期并且是否满足设定格式
12261230
*
12271231
* @param string|mixed $val 日期
1228-
* @param string $format 需要检验的格式数组
1232+
* @param string $format 需要检验的格式数组
12291233
*
12301234
* @return bool
12311235
*/
@@ -1242,9 +1246,9 @@ public static function dateFormat($val, string $format = 'Y-m-d'): bool
12421246
/**
12431247
* 字段值必须是给定日期之前的值
12441248
*
1245-
* @param string|mixed $val
1249+
* @param string|mixed $val
12461250
* @param string|numeric $beforeDate 若为空,将使用当前时间
1247-
* @param string $symbol allow '<' '<='
1251+
* @param string $symbol allow '<' '<='
12481252
*
12491253
* @return bool
12501254
*/
@@ -1270,7 +1274,7 @@ public static function beforeDate($val, $beforeDate = '', string $symbol = '<'):
12701274
/**
12711275
* 字段值必须是小于或等于给定日期的值
12721276
*
1273-
* @param string|mixed $val
1277+
* @param string|mixed $val
12741278
* @param string|numeric $beforeDate
12751279
*
12761280
* @return bool

test/IssuesTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Inhere\ValidateTest;
4+
5+
use Inhere\Validate\Validation;
6+
use function vdump;
7+
8+
/**
9+
* class IssuesTest
10+
*/
11+
class IssuesTest extends BaseValidateTestCase
12+
{
13+
public function testIssue_37(): void
14+
{
15+
$post = ['birthDay' => '1960-09-21'];
16+
17+
$v = Validation::check($post, [
18+
['birthDay', 'required'],
19+
['birthDay', 'date'],
20+
]);
21+
22+
$this->assertTrue($v->isOk());
23+
24+
// must > 0
25+
$v = Validation::check($post, [
26+
['birthDay', 'required'],
27+
['birthDay', 'date', true],
28+
]);
29+
30+
$this->assertTrue($v->isFail());
31+
}
32+
}

0 commit comments

Comments
 (0)