我正在尝试创建一个用于验证时间变量日期和时间的控制器。
问题在于代码可以正确地验证日期和签入时间,但对于签出时间始终会得到错误的值。
$checkOut
的每个变量总是以Not Valid, Wrong Check Out Time Format
验证。
更新:现在,我在这里仅使用2个控制器,扩展checkerController的debug_controller将接收csv文件并检查csv文件中的每一行。
如果有任何无效的数据格式,$result
将包含“格式无效的原因”。使用此值为validateDate($id, $date, $checkIn, $checkOut)
的代码$checkOut
实际上应该为false时,返回false。
奇怪的是,当我使用更少的csv文件行时,最后一行实际上可以返回true。
内部csv文件
198601082010082030,2018-02-30,07:15:01,14:00:00
198601082010082030,2018-06,08:15:01,14:00:00
198601082010082030,2018-06-05,25:15:01,14:00:00
198601082010082030,2018-06-05,08:15,14:00:00
198601082010082030,2018-06-05,08:15:01,14:00:00
198601082010082030,2018-06-06,13:50:32,14:10:00
198601082010082030,2018-06-07,07:50:33,15:10:00
198601082010082030,2018-02-30,07:50:33,15:10:00
198601082010082030,2018-06-07,26:50:33,15:10:00
198601082010082030,2018-06-08,07:50:34,16:10:00
198601082010082030,2018-06-09,07:50:35,15:00:00
198601082010089999,2018-06-10,07:30:00,15:20:00
198601082010082030,2018-06-11,07:30:01,15:00:30
198601082010082030,2018-06-12,07:30:02,15:00:30
198601082010082030,2018-06-13,07:30:03,15:00:30
198601082010082030,2018-06-14,07:30:04,15:00:30
第一个控制器包含检查输入的功能
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use DateTime;
class checkerController extends Controller {
function validateTime($time)
{
$d = DateTime::createFromFormat('H:i:s', $time);
return $d && $d->format('H:i:s') == $time;
}
function validateDate($id, $date, $checkIn, $checkOut)
{
$d = DateTime::createFromFormat('Y-m-d', $date);
$valid = ($d && $d->format('Y-m-d') == $date);
if ($valid == false) {
$data = "Not Valid, Wrong Date Format";
return $data;
} else {
$validCheckOut = $this->validateTime("$checkOut");
if ($validCheckOut == false) {
return "checkout not valid --> $checkOut";
} else {
return "checkout valid";
}
}
}
}
第二个控制器,获取请求的csv文件,继承父checkerController
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Str;
use Storage;
class debug_controller extends checkerController {
function index(request $request){
$ext = $request->file('csv')->getClientOriginalExtension();
$filename = Str::random(10).".$ext";
$path=$request->file('csv')->storeAs('public/csv/', $filename);
$dir = asset("storage/csv/$filename");
$file = file("$dir");
$arrContent = Array();
foreach ($file as $key) {
list($id,$date,$checkIn,$checkOut)=explode(",", $key);
$result = $this->validateDate($id,$date,$checkIn,$checkOut);;
$arrtemp = array('id'=> $id, 'date' => $date, 'checkIn' => $checkIn, 'checkOut' => $checkOut, 'result' => $result);
array_push($arrContent, $arrtemp);
}
foreach ($arrContent as $row) {
echo $row['id']." | ".$row['date']." | ".$row['checkIn']." | ".$row['checkOut']." | ".$row['result']."<br><br>";
}
}
}
?>
答案 0 :(得分:0)
问题出在输入内。
198601082010082030,2018-02-30,07:15:01,14:00:00
198601082010082030,2018-06,08:15:01,14:00:00
198601082010082030,2018-06-05,25:15:01,14:00:00
198601082010082030,2018-06-05,08:15,14:00:00
198601082010082030,2018-06-05,08:15:01,14:00:00
198601082010082030,2018-06-06,13:50:32,14:10:00
198601082010082030,2018-06-07,07:50:33,15:10:00
198601082010082030,2018-02-30,07:50:33,15:10:00
198601082010082030,2018-06-07,26:50:33,15:10:00
198601082010082030,2018-06-08,07:50:34,16:10:00
198601082010082030,2018-06-09,07:50:35,15:00:00
198601082010089999,2018-06-10,07:30:00,15:20:00
198601082010082030,2018-06-11,07:30:01,15:00:30
198601082010082030,2018-06-12,07:30:02,15:00:30
198601082010082030,2018-06-13,07:30:03,15:00:30
198601082010082030,2018-06-14,07:30:04,15:00:30
要获取列的每个值,我使用list($id,$date,$checkIn,$checkOut)=explode(",", $key);
将每个列分配给一个变量。
问题是爆炸的最后一个值没有定界符的,
。因此,即使值是完全相同的字符串,由于某种原因,它也总是返回false。
将逗号添加到最后一个值时,代码可以很好地验证。
这是添加了逗号最后值的输入:
198601082010082030,2018-02-30,07:15:01,14:00:00,
198601082010082030,2018-06,08:15:01,14:00:00,
198601082010082030,2018-06-05,25:15:01,14:00:00,
198601082010082030,2018-06-05,08:15,14:00:00,
198601082010082030,2018-06-05,08:15:01,14:00:00,
198601082010082030,2018-06-06,13:50:32,14:10:00,
198601082010082030,2018-06-07,07:50:33,15:10:00,
198601082010082030,2018-02-30,07:50:33,15:10:00,
198601082010082030,2018-06-07,26:50:33,15:10:00,
198601082010082030,2018-06-08,07:50:34,16:10:00,
198601082010082030,2018-06-09,07:50:35,15:00:00,
198601082010089999,2018-06-10,07:30:00,15:20:00,
198601082010082030,2018-06-11,07:30:01,15:00:30,
198601082010082030,2018-06-12,07:30:02,15:00:30,
198601082010082030,2018-06-13,07:30:03,15:00:30,
198601082010082030,2018-06-14,07:30:04,15:00:30,