Yii Framework Forum: Yii - Date transformations methods - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Yii - Date transformations methods Rate Topic: -----

#1 User is offline   mem 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 16-December 11

Posted 03 July 2012 - 06:40 PM

Hello again,

Can I please ask your help in order to find the proper method to use on this workflow:

1)

The user inserts a date using the following format: dd/mm/yyyy
That date is inserted into the mySQL table date field type as: yyyy/mm/dd

So, and please correct me if I'm wrong, we need to transform the date format from dd/mm/yyyy to yyyy/mm/dd

Question 1)
What yii method (if any) should we use ?


2)
When we display the date back to the user we should displayed in two ways according to the model or controller in question:

2.1)
On an update form field or any other edit operation, it should appear as:
dd/mm/yyyy

Question 2.1)
I believe this would be the same method as used in 1 ?


2.2)
On other views it should:
2.2.1) split the day and the month.

Question 2.2.1
What method can we use to split it ?


2.2.2)
The day should be in numbers;

2.2.3)
The month should be full name (ie. January) but translated (ie. Janvier)

Question 2.2.3
What method can we use to, from the above, return the full month name on a given
language?


I don't need a full example with all the job done but, at least a finger pointer to specific methods then would help me have the job done please.

Thanks in advance,
mem
0

#2 User is offline   Rajith R 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 550
  • Joined: 20-April 11
  • Location:Kochi , Kerala, India

Posted 04 July 2012 - 12:09 AM

before u save use like this
$model->ur-attribute-date=date('Y-m-d',strtotime($model->ur-attribute-date));

1st issue solved.

to display this date use reverse method.
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#3 User is offline   Rajith R 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 550
  • Joined: 20-April 11
  • Location:Kochi , Kerala, India

Posted 04 July 2012 - 12:13 AM

use date('Y',strtotime($model->ur-attribute-date)); - for Year

like for month date('m',strtotime($model->ur-attribute-date));

if u need to translate, use check for translate components and add t() function.
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#4 User is offline   Jimlam 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 195
  • Joined: 17-March 12

Posted 04 July 2012 - 12:50 AM

View PostRajith R, on 04 July 2012 - 12:09 AM, said:

before u save use like this
$model->ur-attribute-date=date('Y-m-d',strtotime($model->ur-attribute-date));

1st issue solved.

to display this date use reverse method.


Hi Rajith,

I need to compare 2 dates: one taken from mysql table (so in the format yyyy-mm-dd) and the other one is a string retrieved from an input in a form but the format is still yyyy-mm-dd. How do I compare the 2 dates?
Thanks
0

#5 User is offline   Rajith R 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 550
  • Joined: 20-April 11
  • Location:Kochi , Kerala, India

Posted 04 July 2012 - 05:57 AM

using
date('Y-m-d',strtotime($model->ur-attribute-date));
this u can convert it to any format .. and compare simply using ' =, <, > ' ...
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#6 User is offline   Rajith R 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 550
  • Joined: 20-April 11
  • Location:Kochi , Kerala, India

Posted 04 July 2012 - 06:03 AM

use like this

if(strtotime($date->target_date)>=strtotime($list['Date']))
or 
if(strtotime($date->target_date)=strtotime($list['Date']))
or 
if(strtotime($date->target_date)<strtotime($list['Date']))

Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#7 User is offline   Jimlam 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 195
  • Joined: 17-March 12

Posted 04 July 2012 - 06:10 AM

View PostRajith R, on 04 July 2012 - 06:03 AM, said:

use like this

if(strtotime($date->target_date)>=strtotime($list['Date']))
or 
if(strtotime($date->target_date)=strtotime($list['Date']))
or 
if(strtotime($date->target_date)<strtotime($list['Date']))



Thanks Rajith,

My problem is that I am using it in the model->find() method and here I cannot use strtotime since it is php. How to solve this?
0

#8 User is offline   Rajith R 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 550
  • Joined: 20-April 11
  • Location:Kochi , Kerala, India

Posted 04 July 2012 - 07:00 AM

so write as criteria

$criteria->condition=$criteria->condition.' and '.'date_of_birth > :date_of_birth';
$criteria->params[':date_of_birth'] = date('Y-m-d',strtotime($_REQUEST['Students']['date_of_birth']));

Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#9 User is offline   Rajith R 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 550
  • Joined: 20-April 11
  • Location:Kochi , Kerala, India

Posted 04 July 2012 - 07:04 AM

View PostBianca, on 04 July 2012 - 06:10 AM, said:

Thanks Rajith,

My problem is that I am using it in the model->find() method and here I cannot use strtotime since it is php. How to solve this?




so u just need to check equal to ?? right.. so change one others format.. using strtotime..then use in find()..
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#10 User is offline   mem 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 16-December 11

Posted 04 July 2012 - 10:46 AM

@Jimlam: Please don't exploit others post. Create your own post for your own doubts. Things get clear this way.

@All:

To retrieve the day and the month I've used two methods on the model:

        /**
         * @desc Given a string, '2012-12-02' returns the day: '02'
         * @param string $date
         * @return string 
         */
        public function getDay($date){            
            return yii::app()->dateFormatter->format('dd', $date);
        }
        
        /**
         * @desc Given a string, '2012-12-02' returns the month: 'December'
         * @param string $date
         * @return string 
         */
        public function getMounth($date){            
            return yii::app()->dateFormatter->format('MMMM', $date);
        }


The date is inserted into the database as yyyy-mm-dd like this:
Let's say: 06/11/2016

protected function beforeSave ()
{
 if(!empty($this->date))
  {
    list($d, $m, $y) = explode('/', $this->date);
    $mk=mktime(0, 0, 0, $m, $d, $y);
    $this->date = date ('Y-m-d', $mk);
  }

 return parent::beforeSave ();
}


On my form (update) I'm getting this:
20/16/1106

How can I fix this ?
0

#11 User is offline   Jimlam 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 195
  • Joined: 17-March 12

Posted 04 July 2012 - 12:43 PM

View Postmem, on 04 July 2012 - 10:46 AM, said:

@Jimlam: Please don't exploit others post. Create your own post for your own doubts. Things get clear this way.

@All:

To retrieve the day and the month I've used two methods on the model:

        /**
         * @desc Given a string, '2012-12-02' returns the day: '02'
         * @param string $date
         * @return string 
         */
        public function getDay($date){            
            return yii::app()->dateFormatter->format('dd', $date);
        }
        
        /**
         * @desc Given a string, '2012-12-02' returns the month: 'December'
         * @param string $date
         * @return string 
         */
        public function getMounth($date){            
            return yii::app()->dateFormatter->format('MMMM', $date);
        }


The date is inserted into the database as yyyy-mm-dd like this:
Let's say: 06/11/2016

protected function beforeSave ()
{
 if(!empty($this->date))
  {
    list($d, $m, $y) = explode('/', $this->date);
    $mk=mktime(0, 0, 0, $m, $d, $y);
    $this->date = date ('Y-m-d', $mk);
  }

 return parent::beforeSave ();
}


On my form (update) I'm getting this:
20/16/1106

How can I fix this ?


@mem: Sorry, I meant no harm. Since I was having a sort of similar problem, I thought it could be beneficial to all those concerned. Anyway, I have up to now noticed a very friendly and helpful attitude from all those who contribute to this community. My sincere apologies for having 'exploited' your post.

BTW, I think your problem could be solved by including in your model an afterFind() method, thus:

	protected function afterFind ()
  	{
  		if($this->date <> '')
  			{
  				// mise en forme de date
  				list($y, $m, $d) = explode('/', $this->date);
  				$mk=mktime(0, 0, 0, $m, $d, $y);
  				$this->date = date ('d/m/Y', $mk);

  	return parent::afterFind ();
	}



Hope it helps
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users