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: Today, 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: Today, 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

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




Theme © iAndrew 2016 - Forum software by © MyBB