Time validation always return false. PHP DateTime, Laravel

Multi tool use


Time validation always return false. PHP DateTime, Laravel
I'm trying to create a controller for validation for time variable date and time.
The problem is the code can validate the date and check in time correctly but always getting false value for check out time.
Every variable of $checkOut
always ended validate as Not Valid, Wrong Check Out Time Format
.
$checkOut
Not Valid, Wrong Check Out Time Format
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 {
$validCheckIn = $this->validateTime($checkIn);
$validCheckOut = $this->validateTime($checkOut);
if ($validCheckIn == false) {
$data = "Not Valid, Wrong Check In Time Format";
return $data;
}
if ($validCheckOut == false) {
$data = "Not Valid, Wrong Check Out Time Format";
return $data;
}
return $this->setTimezone($id, $date, $checkIn, $checkOut);
}
}
($nip, $tanggal, $masuk, $keluar)
actually all the variables is in my native language, take some time for changing all the variables to english but i will try to change it only for this post on stackoverflow.
– Rayhan Adri
19 mins ago
I already changed all variables name sir @MarcinOrlowski
– Rayhan Adri
14 mins ago
What is the type of
$date
parameter in validateDate
method? It is string
right? Then how you think a DateTime
object will be equal of a string
?– MASh
17 secs ago
$date
validateDate
string
DateTime
string
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
side note: get rid of bad habbit of mixing languages in the code. You got English named variables but also
($nip, $tanggal, $masuk, $keluar)
. Stick to one language (best, English)– Marcin Orlowski
24 mins ago