![]() |
CI4 validation greater_than on date field - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: CI4 validation greater_than on date field (/showthread.php?tid=92764) |
CI4 validation greater_than on date field - pippuccio76 - 04-20-2025 HI , can i validate date with grater than ? this is my validation : Code: 'data_tour' => [ and this is the validation (not passed) : Data Partenza deve essere maggiore di oggi2025-04-21|2025-04-20 2025-04-21 is bigger than 2025-04-20... RE: CI4 validation greater_than on date field - grimpirate - 04-20-2025 Convert your date into a unix timestamp after validating it once as a valid date and then perform the greater_than validation. Alternatively, you could pass your dates in a specific format 'Ymd' and the comparison would work. RE: CI4 validation greater_than on date field - pippuccio76 - 04-20-2025 I create a custom rules: Code: <?php solved RE: CI4 validation greater_than on date field - InsiteFX - 04-20-2025 PHP Code: // PHP Method to compare dates RE: CI4 validation greater_than on date field - grimpirate - 04-22-2025 @pippuccio76 : You do not need to return the booleans true or false, you can return $date_to_control > $today which evaluates to a boolean. @InsiteFX :Your code is erroneous InsiteFX. The second parameter of the function doesn't have a $ in front of date2, $date1 > $date2 performs a string comparison not a date comparison and finally applying the ->format method to a string throws a fatal exception. This code is exceedingly error prone. As an aside, whenever you're comparing dates you should use the DateTimeImmutable class rather than DateTime. RE: CI4 validation greater_than on date field - InsiteFX - 04-22-2025 Wasn.t my code I got it off the web to show him another way of doing it. Thank you for pointing the error out. All fixed now. |