Welcome Guest, Not a member yet? Register   Sign In
CI4 validation greater_than on date field
#1

HI ,

can i validate date with grater than ? 
this is my validation :
Code:
                'data_tour' => [
                    'label' => 'Data Partenza',
                    'rules' => 'required|valid_date[Y-m-d]|greater_than[' . date('Y-m-d') . ']',
                    'errors' => [
                        'required' => '{field} obbligatorio',
                        'valid_date' => ' {field} deve essere una data valida',
                        'greater_than' => ' {field} deve essere maggiore di oggi'.$post['data_tour'].'|'. date('Y-m-d'),                   
                    ]

                ],

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...
Reply
#2

(This post was last modified: 04-20-2025, 07:42 AM by grimpirate.)

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.
Reply
#3

(This post was last modified: 04-20-2025, 08:14 AM by pippuccio76.)

I create a custom rules:

Code:
<?php

namespace App\Validation;

class DateRules
{

    /**
    * Check if the first date is bigger than the second date.
    *
    * @param string $today oggi (Y-m-d format).
    * @param string $date_to_control data da controllare(Y-m-d format).
    * @return bool
    *
    */

    public function date_bigger_than_today(string $date_to_control)
    {

        $today = strtotime("now");
        $date_to_control = strtotime($date_to_control);

        if ($date_to_control > $today) {

            return true;

        } else {
           
            return false;
        }

    }

solved
Reply
#4

(This post was last modified: 04-22-2025, 11:19 PM by InsiteFX. Edit Reason: Fixed missing $ sign )

PHP Code:
// PHP Method to compare dates

// Declare two dates in different
// format and use DateTime() function
// to convert date into DateTime
$date1 = new DateTime("12-11-24");
$date2 = new DateTime("2011-03-26");


// Compare the two dates
if ( ! function_exists('compareDates'))
{
    function compareDates(string $date1string $date2): string
    
{
        if ($date1 $date2) {
            return $date1->format("Y-m-d") . " is greater than " $date2->format("Y-m-d");
        } else {
            return $date1->format("Y-m-d") . " is less than " $date2->format("Y-m-d");
        }
    }

What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

@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.
Reply
#6

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB