Save date array in db

So, I have this element in my form:

<?php
$form['order']['date'] = array(
        '#type'          => 'date',
        '#title'         => 'Order date',
      );

In the submit function form, I have an array like this:

[values] => Array
    (
        [date] => Array
            (
                [year] => 2010
                [month] => 11
                [day] => 27
            )
    )

I am looking for an easy way to store this array in a database, and then I want to get this date from the database, which will be replaced back to the editing form.

Are there drupal api functions for this?

+3
source share
7 answers

I think I found an easy way to do this, but does not use the Drupal api.

$date = implode($form_state['values']['date']); // $date = 20101127;
-1
source

I think the Date module will provide you with API calls for this.

, , , PHP DateTime:

$date_arr = $form_state['values']['date'];
if ($date_arr['year'] && $date_arr['month'] && $date_arr['day']) {
  $date = new DateTime();
  $date->setDate($date_arr['year'], $date_arr['month'], $date_arr['day']);
} else {
  $date = NULL;
}

( UNIX, Drupal), $date- > getTimestamp().

, , - :

$query = db_query("select mydate from table");
while ($fields = db_fetch_array($query)) {
  $date = new DateTime();
  $date->setTimestamp($fields[0]);
  $form['order']['date'] = array(
    '#type'          => 'date',
    '#title'         => 'Order date',
    '#default_value' => array(
      year  => $date->format('Y'),
      month => $date->format('m'),
      day   => $date->format('d'),
    ),
  );
}

, .

, !

+2

serialize . , , unserialize , .

//in your submit function
$date = serialize($values['date']);
$sql = "UPDATE foobar SET date = '%s' WHERE id = '%d'";
db_query($sql, $date, $some_id);

//in your form function
$date_array = db_result(db_query("SELECT date FROM foo WHERE id = '%d'", $some_id));
$form['date'] = array(
  '#type' => 'date',
  '#title' => 'Order date',
  '#default_value' => $date_array,  
);
+1

, , . - ?

API, , , ISO, .

0

Drupal (un) serialize date db, , . serialize db, Drupal .

0

Drupal UNIX. UNIX, mktime - .

:

$my_date = mktime(23, 59, 59, $order->values['date']['month'], $order->values['date']['day'], $order->values['date']['year']);

$my_date / .

0

Source: https://habr.com/ru/post/1777179/


All Articles