Y
Y
Yaroslav Studenikin2020-07-24 16:02:39
Yii
Yaroslav Studenikin, 2020-07-24 16:02:39

Yii2. How to store date in db in unix format?

Hello. There is a field with a choice of the publication date of the article in the View:

<div class="date-picker-wrapper">
                        <?= $form->field($model, 'publish_at')->widget(DateTimePicker::class); ?>
                    </div>


5f1ad9540fc34282724245.png

My task is to store this date in the database in Unix format, example: 1531136820.
Yii::$app->request->post() sends this date, but when a record is created in the database, it is saved as an int with the number 1.
Tell me. please what am i missing.
Controller:
public function actionCreate()
    {

        $form = new NewsForm();
        if ($form->load(Yii::$app->request->post()) && $form->validate() )
        {

            $news = $this->service->create($form);
            //print ('<pre>');var_dump($news);die();
            addAlert('success', 'Новость добавлена');
            return $this->redirect(['view', 'id' => $news->id]);
        }

        return $this->render('create', [
            'model' => $form,
        ]);
    }

News Model:
* @property int $id
 * @property integer $created_at
 * @property integer $publish_at
 * @property string $title
 * @property string $short_desc
 * @property string $body
 * @property boolean $is_public
 * @property integer $photo_id
 * @property integer $user_id
 *
 * @property Meta $meta
 * @property Photo $photo
 * @property User $author
 */
class News extends ActiveRecord
{

    public $meta;

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'shop_news';
    }

    public static function create( $title, $short_desc, $body, $publish_at, $is_public, Meta $meta): self
    {
        $news = new static();
        $news->title = $title;
        $news->short_desc = $short_desc;
        $news->meta = $meta;
        $news->body = $body;
        $news->created_at = time();
        $news->publish_at = $publish_at;
        $news->is_public = $is_public;
        $news->user_id = Yii::$app->user->identity->getId();
        return $news;
    }

    public function rules()
    {
        return [
            [['title', 'short_desc', 'body'], 'required'],
            [['title'], 'string', 'max' => 65],
            [['created_at', 'publish_at'], 'safe'],
            //[['publish_at'], 'datetime', 'format' => 'php:d.m.Y H:i', 'timestampAttribute' => 'publish_at'],
            [['short_desc'], 'string', 'max' => 100],
            [['body'], 'string'],
        ];
    }

    public function beforeSave($insert)
    {
        DateTime::createFromFormat('yyyy/mm/dd hh:ii', $this->publish_at)->getTimestamp();
        return parent::beforeSave($insert);
    }

newsservice:
public function create(NewsForm $form): News
    {
        $news = News::create(
            $form->title,
            $form->short_desc,
            $form->body,
            $form->is_public,
            $form->publish_at,
            new Meta(
                $form->meta->title,
                $form->meta->description,
                $form->meta->keywords
            )
        );

Thanks in advance!

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
D
Dmitry, 2020-07-29
@yar_stun

If the date comes in the form 2020-06-12 14:45, then in the validation rules you can write like this.

['publish_at', 'datetime', 'timestampAttribute' => 'publish_at', 'format' => 'php:Y-m-d H:i'],

On the example of contact form validation.
public function contact($email)
    {
        echo $this->publish_at; // 2020-06-12 14:45.
        if ($this->validate()) {
            echo $this->publish_at;  // 1591962300
            exit();
        }
        return false;
    }

Z
Zhandos Timabay, 2020-07-25
@jake47

If the string 2020-06-12 14:45 comes to the controller, then try wrapping the strtotime function in php when assigning as follows: $this->publish_at = strtotime( $news->publish_at);
It should work

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question