From 3739ebb9e70d7515fd9d2bcea38ee6b2071f171b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=83=E5=BD=A9=E6=9E=AB=E5=8F=B6?= <424235748@qq.com> Date: Fri, 19 May 2023 03:23:16 +0000 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=B1=BB=E5=9E=8Bdatetime=20?= =?UTF-8?q?=E6=8B=BC=E8=A3=85sql=E5=87=BA=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 七彩枫叶 <424235748@qq.com> --- app/AdminController.php | 47 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/app/AdminController.php b/app/AdminController.php index 778d1c14..ee20800a 100644 --- a/app/AdminController.php +++ b/app/AdminController.php @@ -452,7 +452,6 @@ class AdminController extends BaseController break; case 'date'; case 'time'; - case 'datetime'; $value = str_replace(',', '-', $value); if (strpos($value, '-')) { @@ -475,6 +474,31 @@ class AdminController extends BaseController $where[] = [$field, '=', $value]; } + break; + case 'datetime'; + $value = str_replace(',', '-', $value); + if (strpos($value, '-')) { + $arr = explode(' - ', $value); + + if (!array_filter($arr)) { + continue 2; + } + if ($arr[0] === '') { + $exp = '<= TIME'; + $arr = $arr[1]; + } elseif ($arr[1] === '') { + $exp = '>= TIME'; + $arr = $arr[0]; + }else{ + $arr[0]=self::format_datetime($arr[0]); + $arr[1]=self::format_datetime($arr[1],'+'); + $where[] = [$field, '>= TIME', $arr[0]]; + $where[] = [$field, '<= TIME', $arr[1]]; + } + + } else { + $where[] = [$field, '=', $value]; + } break; case 'blob'; break; @@ -551,4 +575,25 @@ class AdminController extends BaseController return \response($template, $code); } + + /** + * 格式化datetime + * @param $day_time string 日期时间 字符串 + * @param $op string 进行补全操作方向 + */ + protected function format_datetime(string $day_time, string $op='-'): string + { + if(strtotime($day_time) && strlen($day_time)==10) + { + if($op=='+') + { + return $day_time . " 23:59:59"; + } + return $day_time . " 00:00:00"; + }elseif(strtotime($day_time)){ + return $day_time; + }else{ + return $this->error('传入的变量值 不是时间格式字符串'); + } + } } \ No newline at end of file -- Gitee