N
N
name_a2014-01-13 11:20:49
Yii
name_a, 2014-01-13 11:20:49

What is the best way to organize date formatting in yii and at what stages to do it?

There is a sql server with its datetime format 2014-01-13 11:40:34.423
There is a need to edit the date and time in some forms and save the changes.
The user needs to give the human date and time format 01/13/2014 and 11:40:34, and save it in the database in datetime format.
What is the best way to organize date formatting and at what stages to do it?

Answer the question

In order to leave comments, you need to log in

6 answer(s)
A
Alexander Zelenin, 2014-01-13
@name_a

I store int.
When entering into fields when creating a new entity or searching / filtering a human date (Ymd) in beforeValidate, I translate to unixtime.
When outputting unixtime, I translate to Ymd

public function beforeValidate()
  {
    $attrs = [ 'time', 'testing', 'call' ];
    foreach ( $attrs as $attr ) {
      $time = \DateTime::createFromFormat( 'Y-m-d H:i:s', $this->$attr );
      if ( !$time ) continue;
      $this->$attr = $time->format( 'U' );
    }

    parent::beforeValidate();
    return true;
  }

E
Eugene, 2014-01-13
@Nc_Soft

On withdrawal, of course. It is possible even on js.

G
Grigory Peretyaka, 2014-01-13
@Peretyaka

There is CDateFormatter .
But if DateTime is enough for you, then there is nothing criminal in using it. Theoretically, this will complicate design changes and localization, but in practice, everything still has to be shoveled.
Of course, it is better to do date conversion in the template, where it is displayed.

O
Oleg Titarenko, 2015-02-17
@Urukhayy

SELECT DISTINCT table FROM from_table GROUP BY table

O
Oleg Krasnov, 2015-02-17
@OKrasnov

Can:
SELECT DISTINCT field_1, field_2 FROM table

K
KorsaR-ZN, 2015-02-17
@KorsaR-ZN

What's the problem?
Select by condition ( WHERE ) all rows with the desired type , then group ( GROUP BY ) by type and sort ( ORDER BY ) by primary key.
Well, or through DISTINCT

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question