Explode Data Gives Me Offset, Why?

Hello guys! I’m from Portugal and so we have a different date format: dd/mm/YYYY

Si I need to validate if user has more than 18 years old, and I have a AtiveForm. I have this in _form:




<div class="span6">

            <?php

            echo $form->label($utilizador, 'dataNascimento'),

            $form->dateField($utilizador, 'dataNascimento'),

            $form->error($utilizador, 'dataNascimento');

            ?> 

        </div>



And in controller I have this on rules:





public function rules() {

        return array(

            //..

            array('dataNascimento', 'date', 'format' => 'dd/mm/YYYY'),

            array('dataNascimento', 'idade'),

            //..

        );

    }



And I have a funcion "idade" in controller to:




public function idade($attribute, $params) {

        $datafinal = explode('/', $this->dataNascimento);

        $datafinal = ($datafinal[2] . '-' . $datafinal[1] . '-' . $datafinal[0]);


        $diferencaDatas = time() - strtotime($datafinal);


        if ($diferencaDatas < 18) {

            $this->addError('dataNascimento', 'O utilizador é menor de idade!');

        }

    }



I tried a similiar code to test the ideia and it works (in a website to test php code)

but in my app I have an issue:

Undefined offset: 2

Why? Can anyone help me?

The main ideia is to see if user is more than 18. But I have to convert y portuguese date to english format. So I use explode function to convert date and is that convertion I have this error.

Thanks a lot! :)

It’s better to use date like this:

main.php




'components' => array(

	// ...others

	'format' => array(

		'dateFormat' => 'd.m.Y',

		'datetimeFormat' => 'd.m.Y, H:i:s'

	),

),



in you model rules:


array( 'date', 'date', 'format' => Yii::app()->locale->dateFormat ),

If you add custom rule you neet to place it in model.




const MINIMUN_AGE = 18;


public function idade( $attribute, $params ){

	$timestamp = CDateTimeParser::parse( $this->$attribute, Yii::app()->locale->dateFormat );

	if ( $this->getAge( $timestamp ) < self::MINIMUM_AGE ) {

		$this->addError('dataNascimento', 'O utilizador é menor de idade!');

	}

}


function getAge( $birth ){

	$t = time();

	$age = ($birth < 0) ? ( $t + ($birth * -1) ) : $t - $birth;

	return floor($age/31536000);

}



I didn’t test code for errors, so you do.

I think you need to explode $params:




        $datafinal = explode('/', $params['dataNascimento']);



It gives me "Undefined index: dataNascimento"

in this line


   $datafinal = explode('/', $params['dataNascimento']);

I will try SiZE response, and give a feedback.

I get this error:

"The format of Data de Nascimento is invalid."

When I insert a simple date to DB (from a dateField) it is in this format: YYYY-mm-dd

So waht I did is, went to main.php and change this




'components' => array(

	// ...others

	'format' => array(

		'dateFormat' => 'd.m.Y',

		'datetimeFormat' => 'd.m.Y, H:i:s'

	),

),



to this


format' => array('dateFormat' => 'YYYY-mm-dd',

                        'datetimeFormat' => 'YYYY-mm-dd, H:i:s'),

But I still have the same error…

I’m so confused!

When I put a date from a dateField it is in this format: dd-mm-YYYY

The same date is storage in DB with this format: YYYY-mm-dd

And now? Which format should I consider?

I gave you working example from my project.

First of all you need to reed some docs, whey are not hidden from developers ))) http://www.yiiframework.com/doc/api/1.1/CFormatter#dateFormat-detail

Your format YYYY-mm-dd equals to string 201420142014-0404-2626

UPD: date format in Yii::app()->locale->dateFormat depends on param language in config, for me it’s:




'sourceLanguage' => 'en_US',

'language' => 'ru',



so


echo Yii::app()->locale->dateFormat;

// dd.MM.yyyy

Ok, I’m understandig now…

At this this moment I get your ideia. Now I just complete your code, because it’s not verifying birth date. It accepts all birth dates :confused: but now I saw my (date format) error :)