Yii - Date transformations methods
#1
Posted 03 July 2012 - 06:40 PM
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
#2
Posted 04 July 2012 - 12:09 AM
$model->ur-attribute-date=date('Y-m-d',strtotime($model->ur-attribute-date));1st issue solved.
to display this date use reverse method.
Wiwo inc.
| Mobile: 919995504508
#3
Posted 04 July 2012 - 12:13 AM
like for month date('m',strtotime($model->ur-attribute-date));
if u need to translate, use check for translate components and add t() function.
Wiwo inc.
| Mobile: 919995504508
#4
Posted 04 July 2012 - 12:50 AM
Rajith R, on 04 July 2012 - 12:09 AM, said:
$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
#5
Posted 04 July 2012 - 05:57 AM
date('Y-m-d',strtotime($model->ur-attribute-date)); this u can convert it to any format .. and compare simply using ' =, <, > ' ...
Wiwo inc.
| Mobile: 919995504508
#6
Posted 04 July 2012 - 06:03 AM
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']))
Wiwo inc.
| Mobile: 919995504508
#7
Posted 04 July 2012 - 06:10 AM
Rajith R, on 04 July 2012 - 06:03 AM, said:
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?
#8
Posted 04 July 2012 - 07:00 AM
$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']));
Wiwo inc.
| Mobile: 919995504508
#9
Posted 04 July 2012 - 07:04 AM
Bianca, on 04 July 2012 - 06:10 AM, said:
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()..
Wiwo inc.
| Mobile: 919995504508
#10
Posted 04 July 2012 - 10:46 AM
@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 ?
#11
Posted 04 July 2012 - 12:43 PM
mem, on 04 July 2012 - 10:46 AM, said:
@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

Help













