Yii Framework Forum: Working with DATETIME fields via ActiveRecord - Yii Framework Forum

Jump to content

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

Working with DATETIME fields via ActiveRecord Rate Topic: -----

#1 User is offline   Nayjest 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 85
  • Joined: 05-June 10
  • Location:Ukraine, Kiev

Posted 10 June 2010 - 10:59 AM

Dates in examples (The Yii Blog Tutorial) are stored in DB as integers.
I think it isnt best practice. I wont to store dates in DATETIME format.
But formatting date by php function date() with hardcoded date format in model when i wont to store data isnt best practice too coz some DBMS may use different date format.
What is best way to store date to DB in DATETIME format using CActiveRecord?
Web-superman. My blog about web development (russian).
0

#2 User is offline   MadSkillsTisdale 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 101
  • Joined: 15-February 09
  • Location:New Orleans, LA

Posted 10 June 2010 - 03:43 PM

In your main config file, add an application parameter for whatever dat format you prefer



.....
'params'=>array(
'myDateFormat' => 'Y-m-d',
.....




Then you can use php date like this

......
$myModel->myDate = date(Yii::app()->params['myDateFormat']);
......
0

#3 User is offline   Nayjest 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 85
  • Joined: 05-June 10
  • Location:Ukraine, Kiev

Posted 10 June 2010 - 03:47 PM

I added param to application config:
'params'=>array(		
        'dbDateFormat'=>'Y-m-d h:m:s'
	)

and use date(Yii::app()->params['dbDateFormat']);
But i dont like that this param is not in DB configuration (CDBConnection has no such accesible params) and still think that its not best way
Web-superman. My blog about web development (russian).
0

#4 User is offline   jonah 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 733
  • Joined: 27-November 08
  • Location:California (US)

Posted 10 June 2010 - 04:25 PM

This is what I do:

<?php
/**
* Helps bridge the gap between php 5.2 and 5.3 DateTime class and also implants
* __toString() to spit out a MySQL datetime string
*/
class EDateTime extends DateTime {
	public static $DBFormat = 'Y-m-d H:i;s';

	public function getTimestamp() {
		return method_exists('DateTime', 'getTimestamp') ?  parent::getTimestamp() : $this->format('U');
	}
	
	function setTimestamp($timestamp) {
		if (method_exists('DateTime', 'setTimestamp')) parent::setTimestamp($timestamp);
		
		$thisz_original = $this->getTimezone()->getName();
		$thisz_utc = new DateTimeZone('UTC');
		$this -> setTimezone($thisz_utc);
		$year = gmdate("Y",$timestamp);
		$month = gmdate("n",$timestamp);
		$day = gmdate("j",$timestamp);
		$hour = gmdate("G",$timestamp);
		$minute = gmdate("i",$timestamp);
		$second = gmdate("s",$timestamp);
		$this -> setDate($year,$month,$day);
		$this -> setTime($hour,$minute,$second);
		$this -> setTimezone(new DateTimeZone($thisz_original));
	}
	
	public function __toString(){
		return (string)parent::format(self::$DBFormat);
	}
}


$model = new Model;
$model->date = new EDateTime('now');

http://php-thoughts.cubedwater.com - my bloggings about Yii
0

#5 User is offline   PrplHaz4 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 108
  • Joined: 28-September 09
  • Location:Boston, MA

Posted 16 June 2010 - 12:49 AM

I'm just starting to dig in with Yii, and liking it so far. Haven't really committed to either of the proposed solutions yet for dates, but have a few questions:

First, does this mean that every time I set a date before writing it to MySQL (using the Save operation), I must first convert it to the correct format?

Secondly, is it not strange that this would be set by default in the MySQL provider configuration section, so that the framework could instantiate and work with dates in the "preferred" format by default?

Third, Jonah, where does best practice dictate you put utility code like that? Does it live in its own file? Some place special in the directory structure?

Thanks!!
0

#6 User is offline   PrplHaz4 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 108
  • Joined: 28-September 09
  • Location:Boston, MA

Posted 16 June 2010 - 01:07 AM

View PostNayjest, on 10 June 2010 - 03:47 PM, said:

I added param to application config:
'params'=>array(		
        'dbDateFormat'=>'Y-m-d h:m:s'
	)

and use date(Yii::app()->params['dbDateFormat']);
But i dont like that this param is not in DB configuration (CDBConnection has no such accesible params) and still think that its not best way


Follow-up for posterity, I was trying to utilize this method and getting odd results. I had to change the dbDateFormat string as follows (note the i instead of m for minutes):
'params'=>array(		
        'dbDateFormat'=>'Y-m-d H:i:s'
	)


Now, every time I write to the (MYSQL) database, it looks like this:
$this->ModifiedOn=date(Yii::app()->params['dbDateFormat']);

0

#7 User is offline   jonah 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 733
  • Joined: 27-November 08
  • Location:California (US)

Posted 21 June 2010 - 05:08 PM

View PostPrplHaz4, on 16 June 2010 - 12:49 AM, said:


Third, Jonah, where does best practice dictate you put utility code like that? Does it live in its own file? Some place special in the directory structure?

Thanks!!



I personally put it under protected/components, but you can put it anywhere you like as long as the directory is set to auto-include or you manually include it each time
http://php-thoughts.cubedwater.com - my bloggings about Yii
0

#8 User is offline   PrplHaz4 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 108
  • Joined: 28-September 09
  • Location:Boston, MA

Posted 15 July 2010 - 10:07 PM

View Postjonah, on 21 June 2010 - 05:08 PM, said:

I personally put it under protected/components, but you can put it anywhere you like as long as the directory is set to auto-include or you manually include it each time


Thanks Jonah.

For continuity's sake, can a mod rename this thread to have "ActiveRecord" spelled correctly? Will help in finding it through search.
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