Chèn ngày giờ vào php

Câu hỏi thường gặp về ngày/giờ PHP. Làm cách nào để tạo ngày ở định dạng thích hợp để chèn trường Dấu thời gian SQL vào cơ sở dữ liệu SQL?


 

Ghi chú. Bạn có thể không cần tạo ngày PHP

Trước hết, bạn có thể không cần tạo ngày tháng trong PHP như thế này. Nếu bạn đang sử dụng PHP cũ thuần túy và cơ sở dữ liệu như MySQL, bạn có thể sử dụng hàm SQL

2019-10-02 18:41:17
19 để chèn dữ liệu vào trường dấu thời gian SQL như thế này

INSERT INTO projects 
  [user_id, name, last_updated, date_created]
  VALUES [5, 'alvin', now[], now[]];

Tôi vừa thử nghiệm điều này với PHP và MySQL, và nó hoạt động tốt. Vì vậy, đó là một cách để điền vào trường dấu thời gian SQL trong truy vấn SQL INSERT

Tạo biến dấu thời gian PHP

Tuy nhiên, nếu bạn muốn thực hiện tất cả điều này bằng PHP [hoặc cần, tùy thuộc vào khung công tác bạn đang làm việc], bạn có thể lấy ngày và giờ hiện tại ở định dạng phù hợp chỉ bằng cách sử dụng PHP, như sau

$timestamp = date['Y-m-d H:i:s'];

Nếu bạn in nó ra, trường

2019-10-02 18:41:17
20 của bạn bây giờ sẽ chứa nội dung như thế này

2019-10-02 18:41:17

Sau đó, bạn có thể sử dụng chuỗi dấu thời gian được định dạng này trong phần chèn PHP MySQL

Ghi chú. Cảm ơn những người bình luận bên dưới đã đề xuất sử dụng

2019-10-02 18:41:17
21 thay vì
2019-10-02 18:41:17
22

Ví dụ về CHÈN SQL Drupal 7 với Dấu thời gian

Mặc dù đây không phải là câu lệnh

$timestamp = date['Y-m-d H:i:s'];
0 PHP/MySQL tiêu chuẩn có sẵn, nhưng đây là giao diện của một truy vấn SQL
$timestamp = date['Y-m-d H:i:s'];
0 khi tôi sử dụng câu lệnh này với Drupal 7

$project = new stdClass[];
$project->user_id = get_user_id[];
$project->project_count_type = $form_state['values']['type'];
$project->name = $form_state['values']['name'];
$project->description = $form_state['values']['description'];

# get the current time in the proper format for a sql timestamp field
$timestamp = date['Y-m-d H:i:s'];

# new drupal 7 style insert
$id = db_insert['projects']
    ->fields[array[
        'user_id' => $project->user_id,
        'project_count_type' => $project->project_count_type,
        'name' => $project->name,
        'description' => $project->description,
        'last_updated' => $timestamp,
        'date_created' => $timestamp
]]
->execute[];

Như bạn có thể thấy trong các dòng tôi đã in đậm, tôi đang chèn biến dấu thời gian PHP của mình vào hai trường SQL

Nhận dấu thời gian cho một số ngày và giờ khác

Lưu ý rằng hàm PHP

$timestamp = date['Y-m-d H:i:s'];
2 mặc định là ngày và giờ hiện tại, đây chính xác là những gì tôi cần cho mục đích của mình ở đây. Nếu bạn cần tạo trường dấu thời gian được định dạng cho một số ngày và giờ khác, bạn có thể làm như thế này

$timestamp = date['Y-m-d H:i:s', mktime[0, 0, 0, 7, 1, 2000]];

Dưới đây là một số ví dụ PHP

$timestamp = date['Y-m-d H:i:s'];
3 khác

$tomorrow  = mktime[0, 0, 0, date["m"]  , date["d"]+1, date["Y"]];
$lastmonth = mktime[0, 0, 0, date["m"]-1, date["d"],   date["Y"]];
$nextyear  = mktime[0, 0, 0, date["m"],   date["d"],   date["Y"]+1];

Tôi đã lấy những ví dụ đó từ trang ngày PHP. Vui lòng xem trang đó để biết thêm thông tin về cách tạo ngày và giờ khác [tôi hầu như chỉ lo lắng về "bây giờ" vào lúc này]

Chèn dấu thời gian PHP SQL

Tôi hy vọng những ví dụ về dấu thời gian này hữu ích. Như bạn đã thấy, thông thường bạn chỉ có thể sử dụng hàm 'NOW[]' của SQL để chèn vào trường dấu thời gian SQL, nhưng nếu vì lý do nào đó mà cách đó không hoạt động, bạn cũng có thể tạo trường dấu thời gian ở định dạng phù hợp bằng cách sử dụng

Ngày Giờ. Hàm add[] là một hàm có sẵn trong PHP được sử dụng để thêm một lượng thời gian [ngày, tháng, năm, giờ, phút và giây] vào đối tượng DateTime đã cho

cú pháp

  • Phong cách hướng đối tượng.
    $timestamp = date['Y-m-d H:i:s'];
    
    0
  • phong cách thủ tục.
    $timestamp = date['Y-m-d H:i:s'];
    
    1

Thông số. Hàm này sử dụng hai tham số như đã đề cập ở trên và được mô tả bên dưới

  • đối tượng $. Nó chỉ định đối tượng DateTime được trả về bởi hàm date_create[]. Hàm này trả về một đối tượng DateTime mới
  • $khoảng. Tham số này giữ đối tượng DateInterval

Giá trị trả về. Hàm này trả về đối tượng DateTime mới sau khi thay đổi thành công hoặc Sai khi thất bại

Sự cố này mô tả định dạng ngày để chèn ngày vào cơ sở dữ liệu MySQL. MySQL truy xuất và hiển thị các giá trị DATETIME trong 'YYYY-MM-DD HH. MM. định dạng SS. Ngày chỉ có thể được lưu trữ ở định dạng này. Tuy nhiên, nó có thể được sử dụng với bất kỳ chức năng định dạng thời gian nào để thay đổi và hiển thị nó.

Khi viết một truy vấn trong MySQL bằng PHP, khả năng ứng dụng của nó sẽ được kiểm tra trên cơ sở của chính MySQL. Vì vậy, hãy sử dụng định dạng ngày và giờ mặc định do MySQL cung cấp. e. 'YYYY-MM-DD'

ví dụ

$timestamp = date['Y-m-d H:i:s'];
2

Truy vấn MySQL để tạo DataBase

$timestamp = date['Y-m-d H:i:s'];
3

ví dụ 1. Chương trình PHP để tạo cơ sở dữ liệu và bảng

php




2019-10-02 18:41:17
23

2019-10-02 18:41:17
24

2019-10-02 18:41:17
25
$timestamp = date['Y-m-d H:i:s'];
30____131
$timestamp = date['Y-m-d H:i:s'];
32

$timestamp = date['Y-m-d H:i:s'];
33
$timestamp = date['Y-m-d H:i:s'];
30
$timestamp = date['Y-m-d H:i:s'];
35____132

$timestamp = date['Y-m-d H:i:s'];
37
$timestamp = date['Y-m-d H:i:s'];
30
$timestamp = date['Y-m-d H:i:s'];
39
$timestamp = date['Y-m-d H:i:s'];
32

2019-10-02 18:41:17
51
$timestamp = date['Y-m-d H:i:s'];
30
2019-10-02 18:41:17
53____132

2019-10-02 18:41:17
24

2019-10-02 18:41:17
56

2019-10-02 18:41:17
57
2019-10-02 18:41:17
58
2019-10-02 18:41:17
25
$timestamp = date['Y-m-d H:i:s'];
20
$timestamp = date['Y-m-d H:i:s'];
33
$timestamp = date['Y-m-d H:i:s'];
20
$timestamp = date['Y-m-d H:i:s'];
37
$timestamp = date['Y-m-d H:i:s'];
20
2019-10-02 18:41:17
51
$timestamp = date['Y-m-d H:i:s'];
26

2019-10-02 18:41:17
24

$timestamp = date['Y-m-d H:i:s'];
28

$timestamp = date['Y-m-d H:i:s'];
29
$timestamp = date['Y-m-d H:i:s'];
10
2019-10-02 18:41:17
57
$timestamp = date['Y-m-d H:i:s'];
12

$timestamp = date['Y-m-d H:i:s'];
13
$timestamp = date['Y-m-d H:i:s'];
14
$timestamp = date['Y-m-d H:i:s'];
15
$timestamp = date['Y-m-d H:i:s'];
16
$timestamp = date['Y-m-d H:i:s'];
17

$timestamp = date['Y-m-d H:i:s'];
18

2019-10-02 18:41:17
24

$project = new stdClass[];
$project->user_id = get_user_id[];
$project->project_count_type = $form_state['values']['type'];
$project->name = $form_state['values']['name'];
$project->description = $form_state['values']['description'];

# get the current time in the proper format for a sql timestamp field
$timestamp = date['Y-m-d H:i:s'];

# new drupal 7 style insert
$id = db_insert['projects']
    ->fields[array[
        'user_id' => $project->user_id,
        'project_count_type' => $project->project_count_type,
        'name' => $project->name,
        'description' => $project->description,
        'last_updated' => $timestamp,
        'date_created' => $timestamp
]]
->execute[];
00

$project = new stdClass[];
$project->user_id = get_user_id[];
$project->project_count_type = $form_state['values']['type'];
$project->name = $form_state['values']['name'];
$project->description = $form_state['values']['description'];

# get the current time in the proper format for a sql timestamp field
$timestamp = date['Y-m-d H:i:s'];

# new drupal 7 style insert
$id = db_insert['projects']
    ->fields[array[
        'user_id' => $project->user_id,
        'project_count_type' => $project->project_count_type,
        'name' => $project->name,
        'description' => $project->description,
        'last_updated' => $timestamp,
        'date_created' => $timestamp
]]
->execute[];
01
$project = new stdClass[];
$project->user_id = get_user_id[];
$project->project_count_type = $form_state['values']['type'];
$project->name = $form_state['values']['name'];
$project->description = $form_state['values']['description'];

# get the current time in the proper format for a sql timestamp field
$timestamp = date['Y-m-d H:i:s'];

# new drupal 7 style insert
$id = db_insert['projects']
    ->fields[array[
        'user_id' => $project->user_id,
        'project_count_type' => $project->project_count_type,
        'name' => $project->name,
        'description' => $project->description,
        'last_updated' => $timestamp,
        'date_created' => $timestamp
]]
->execute[];
02

_______413____504

_______413____506

$project = new stdClass[];
$project->user_id = get_user_id[];
$project->project_count_type = $form_state['values']['type'];
$project->name = $form_state['values']['name'];
$project->description = $form_state['values']['description'];

# get the current time in the proper format for a sql timestamp field
$timestamp = date['Y-m-d H:i:s'];

# new drupal 7 style insert
$id = db_insert['projects']
    ->fields[array[
        'user_id' => $project->user_id,
        'project_count_type' => $project->project_count_type,
        'name' => $project->name,
        'description' => $project->description,
        'last_updated' => $timestamp,
        'date_created' => $timestamp
]]
->execute[];
07

2019-10-02 18:41:17
24

$timestamp = date['Y-m-d H:i:s'];
29
$project = new stdClass[];
$project->user_id = get_user_id[];
$project->project_count_type = $form_state['values']['type'];
$project->name = $form_state['values']['name'];
$project->description = $form_state['values']['description'];

# get the current time in the proper format for a sql timestamp field
$timestamp = date['Y-m-d H:i:s'];

# new drupal 7 style insert
$id = db_insert['projects']
    ->fields[array[
        'user_id' => $project->user_id,
        'project_count_type' => $project->project_count_type,
        'name' => $project->name,
        'description' => $project->description,
        'last_updated' => $timestamp,
        'date_created' => $timestamp
]]
->execute[];
90
2019-10-02 18:41:17
57____320
$project = new stdClass[];
$project->user_id = get_user_id[];
$project->project_count_type = $form_state['values']['type'];
$project->name = $form_state['values']['name'];
$project->description = $form_state['values']['description'];

# get the current time in the proper format for a sql timestamp field
$timestamp = date['Y-m-d H:i:s'];

# new drupal 7 style insert
$id = db_insert['projects']
    ->fields[array[
        'user_id' => $project->user_id,
        'project_count_type' => $project->project_count_type,
        'name' => $project->name,
        'description' => $project->description,
        'last_updated' => $timestamp,
        'date_created' => $timestamp
]]
->execute[];
01
$project = new stdClass[];
$project->user_id = get_user_id[];
$project->project_count_type = $form_state['values']['type'];
$project->name = $form_state['values']['name'];
$project->description = $form_state['values']['description'];

# get the current time in the proper format for a sql timestamp field
$timestamp = date['Y-m-d H:i:s'];

# new drupal 7 style insert
$id = db_insert['projects']
    ->fields[array[
        'user_id' => $project->user_id,
        'project_count_type' => $project->project_count_type,
        'name' => $project->name,
        'description' => $project->description,
        'last_updated' => $timestamp,
        'date_created' => $timestamp
]]
->execute[];
94

$timestamp = date['Y-m-d H:i:s'];
13
$project = new stdClass[];
$project->user_id = get_user_id[];
$project->project_count_type = $form_state['values']['type'];
$project->name = $form_state['values']['name'];
$project->description = $form_state['values']['description'];

# get the current time in the proper format for a sql timestamp field
$timestamp = date['Y-m-d H:i:s'];

# new drupal 7 style insert
$id = db_insert['projects']
    ->fields[array[
        'user_id' => $project->user_id,
        'project_count_type' => $project->project_count_type,
        'name' => $project->name,
        'description' => $project->description,
        'last_updated' => $timestamp,
        'date_created' => $timestamp
]]
->execute[];
96
$project = new stdClass[];
$project->user_id = get_user_id[];
$project->project_count_type = $form_state['values']['type'];
$project->name = $form_state['values']['name'];
$project->description = $form_state['values']['description'];

# get the current time in the proper format for a sql timestamp field
$timestamp = date['Y-m-d H:i:s'];

# new drupal 7 style insert
$id = db_insert['projects']
    ->fields[array[
        'user_id' => $project->user_id,
        'project_count_type' => $project->project_count_type,
        'name' => $project->name,
        'description' => $project->description,
        'last_updated' => $timestamp,
        'date_created' => $timestamp
]]
->execute[];
97____132

$timestamp = date['Y-m-d H:i:s'];
18
2019-10-02 18:41:17
230
2019-10-02 18:41:17
231

$timestamp = date['Y-m-d H:i:s'];
13
$project = new stdClass[];
$project->user_id = get_user_id[];
$project->project_count_type = $form_state['values']['type'];
$project->name = $form_state['values']['name'];
$project->description = $form_state['values']['description'];

# get the current time in the proper format for a sql timestamp field
$timestamp = date['Y-m-d H:i:s'];

# new drupal 7 style insert
$id = db_insert['projects']
    ->fields[array[
        'user_id' => $project->user_id,
        'project_count_type' => $project->project_count_type,
        'name' => $project->name,
        'description' => $project->description,
        'last_updated' => $timestamp,
        'date_created' => $timestamp
]]
->execute[];
96
2019-10-02 18:41:17
234
2019-10-02 18:41:17
235
2019-10-02 18:41:17
57
$timestamp = date['Y-m-d H:i:s'];
26

$timestamp = date['Y-m-d H:i:s'];
18

2019-10-02 18:41:17
24

2019-10-02 18:41:17
240

2019-10-02 18:41:17
241____257____326

2019-10-02 18:41:17
244


đầu ra.
2019-10-02 18:41:17
5

ví dụ 2. Chương trình PHP để chèn ngày vào bảng

php




2019-10-02 18:41:17
23

2019-10-02 18:41:17
24

2019-10-02 18:41:17
25
$timestamp = date['Y-m-d H:i:s'];
30____131
$timestamp = date['Y-m-d H:i:s'];
32

$timestamp = date['Y-m-d H:i:s'];
33
$timestamp = date['Y-m-d H:i:s'];
30
$timestamp = date['Y-m-d H:i:s'];
35____132

$timestamp = date['Y-m-d H:i:s'];
37
$timestamp = date['Y-m-d H:i:s'];
30
$timestamp = date['Y-m-d H:i:s'];
39
$timestamp = date['Y-m-d H:i:s'];
32

2019-10-02 18:41:17
51
$timestamp = date['Y-m-d H:i:s'];
30
2019-10-02 18:41:17
53____132

2019-10-02 18:41:17
24

2019-10-02 18:41:17
56

2019-10-02 18:41:17
57
2019-10-02 18:41:17
58
2019-10-02 18:41:17
25
$timestamp = date['Y-m-d H:i:s'];
20
$timestamp = date['Y-m-d H:i:s'];
33
$timestamp = date['Y-m-d H:i:s'];
20
$timestamp = date['Y-m-d H:i:s'];
37
$timestamp = date['Y-m-d H:i:s'];
20
2019-10-02 18:41:17
51
$timestamp = date['Y-m-d H:i:s'];
26

2019-10-02 18:41:17
24

$timestamp = date['Y-m-d H:i:s'];
28

$timestamp = date['Y-m-d H:i:s'];
29
$timestamp = date['Y-m-d H:i:s'];
10
2019-10-02 18:41:17
57
$timestamp = date['Y-m-d H:i:s'];
12

$timestamp = date['Y-m-d H:i:s'];
13
$timestamp = date['Y-m-d H:i:s'];
14
$timestamp = date['Y-m-d H:i:s'];
15
$timestamp = date['Y-m-d H:i:s'];
16
$timestamp = date['Y-m-d H:i:s'];
17

$timestamp = date['Y-m-d H:i:s'];
18

2019-10-02 18:41:17
24

$timestamp = date['Y-m-d H:i:s'];
328

$project = new stdClass[];
$project->user_id = get_user_id[];
$project->project_count_type = $form_state['values']['type'];
$project->name = $form_state['values']['name'];
$project->description = $form_state['values']['description'];

# get the current time in the proper format for a sql timestamp field
$timestamp = date['Y-m-d H:i:s'];

# new drupal 7 style insert
$id = db_insert['projects']
    ->fields[array[
        'user_id' => $project->user_id,
        'project_count_type' => $project->project_count_type,
        'name' => $project->name,
        'description' => $project->description,
        'last_updated' => $timestamp,
        'date_created' => $timestamp
]]
->execute[];
01
$timestamp = date['Y-m-d H:i:s'];
330

$timestamp = date['Y-m-d H:i:s'];
331
$timestamp = date['Y-m-d H:i:s'];
332
$timestamp = date['Y-m-d H:i:s'];
333
$timestamp = date['Y-m-d H:i:s'];
334

2019-10-02 18:41:17
24

$timestamp = date['Y-m-d H:i:s'];
29
$project = new stdClass[];
$project->user_id = get_user_id[];
$project->project_count_type = $form_state['values']['type'];
$project->name = $form_state['values']['name'];
$project->description = $form_state['values']['description'];

# get the current time in the proper format for a sql timestamp field
$timestamp = date['Y-m-d H:i:s'];

# new drupal 7 style insert
$id = db_insert['projects']
    ->fields[array[
        'user_id' => $project->user_id,
        'project_count_type' => $project->project_count_type,
        'name' => $project->name,
        'description' => $project->description,
        'last_updated' => $timestamp,
        'date_created' => $timestamp
]]
->execute[];
90
2019-10-02 18:41:17
57____320
$project = new stdClass[];
$project->user_id = get_user_id[];
$project->project_count_type = $form_state['values']['type'];
$project->name = $form_state['values']['name'];
$project->description = $form_state['values']['description'];

# get the current time in the proper format for a sql timestamp field
$timestamp = date['Y-m-d H:i:s'];

# new drupal 7 style insert
$id = db_insert['projects']
    ->fields[array[
        'user_id' => $project->user_id,
        'project_count_type' => $project->project_count_type,
        'name' => $project->name,
        'description' => $project->description,
        'last_updated' => $timestamp,
        'date_created' => $timestamp
]]
->execute[];
01
$project = new stdClass[];
$project->user_id = get_user_id[];
$project->project_count_type = $form_state['values']['type'];
$project->name = $form_state['values']['name'];
$project->description = $form_state['values']['description'];

# get the current time in the proper format for a sql timestamp field
$timestamp = date['Y-m-d H:i:s'];

# new drupal 7 style insert
$id = db_insert['projects']
    ->fields[array[
        'user_id' => $project->user_id,
        'project_count_type' => $project->project_count_type,
        'name' => $project->name,
        'description' => $project->description,
        'last_updated' => $timestamp,
        'date_created' => $timestamp
]]
->execute[];
94

$timestamp = date['Y-m-d H:i:s'];
13
$project = new stdClass[];
$project->user_id = get_user_id[];
$project->project_count_type = $form_state['values']['type'];
$project->name = $form_state['values']['name'];
$project->description = $form_state['values']['description'];

# get the current time in the proper format for a sql timestamp field
$timestamp = date['Y-m-d H:i:s'];

# new drupal 7 style insert
$id = db_insert['projects']
    ->fields[array[
        'user_id' => $project->user_id,
        'project_count_type' => $project->project_count_type,
        'name' => $project->name,
        'description' => $project->description,
        'last_updated' => $timestamp,
        'date_created' => $timestamp
]]
->execute[];
96
$timestamp = date['Y-m-d H:i:s'];
344
$timestamp = date['Y-m-d H:i:s'];
32

$timestamp = date['Y-m-d H:i:s'];
18
2019-10-02 18:41:17
230
2019-10-02 18:41:17
231

$timestamp = date['Y-m-d H:i:s'];
13
$project = new stdClass[];
$project->user_id = get_user_id[];
$project->project_count_type = $form_state['values']['type'];
$project->name = $form_state['values']['name'];
$project->description = $form_state['values']['description'];

# get the current time in the proper format for a sql timestamp field
$timestamp = date['Y-m-d H:i:s'];

# new drupal 7 style insert
$id = db_insert['projects']
    ->fields[array[
        'user_id' => $project->user_id,
        'project_count_type' => $project->project_count_type,
        'name' => $project->name,
        'description' => $project->description,
        'last_updated' => $timestamp,
        'date_created' => $timestamp
]]
->execute[];
96
$timestamp = date['Y-m-d H:i:s'];
351
$timestamp = date['Y-m-d H:i:s'];
352
$project = new stdClass[];
$project->user_id = get_user_id[];
$project->project_count_type = $form_state['values']['type'];
$project->name = $form_state['values']['name'];
$project->description = $form_state['values']['description'];

# get the current time in the proper format for a sql timestamp field
$timestamp = date['Y-m-d H:i:s'];

# new drupal 7 style insert
$id = db_insert['projects']
    ->fields[array[
        'user_id' => $project->user_id,
        'project_count_type' => $project->project_count_type,
        'name' => $project->name,
        'description' => $project->description,
        'last_updated' => $timestamp,
        'date_created' => $timestamp
]]
->execute[];
01
$timestamp = date['Y-m-d H:i:s'];
352
$timestamp = date['Y-m-d H:i:s'];
355
2019-10-02 18:41:17
235
2019-10-02 18:41:17
57
$timestamp = date['Y-m-d H:i:s'];
26

$timestamp = date['Y-m-d H:i:s'];
18

2019-10-02 18:41:17
24

2019-10-02 18:41:17
240

2019-10-02 18:41:17
241____257____326

2019-10-02 18:41:17
244


đầu ra.
$timestamp = date['Y-m-d H:i:s'];
2

Ví dụ 3. Ví dụ này được sử dụng để hiển thị hàng nào được tạo vào ngày 2018-12-05. Sử dụng truy vấn sau để hiển thị kết quả.
Cột created_at không chỉ chứa ngày tháng mà còn cả thời gian. Vì vậy, nó sẽ hiển thị thông báo lỗi.

$timestamp = date['Y-m-d H:i:s'];
1

đầu ra

$project = new stdClass[];
$project->user_id = get_user_id[];
$project->project_count_type = $form_state['values']['type'];
$project->name = $form_state['values']['name'];
$project->description = $form_state['values']['description'];

# get the current time in the proper format for a sql timestamp field
$timestamp = date['Y-m-d H:i:s'];

# new drupal 7 style insert
$id = db_insert['projects']
    ->fields[array[
        'user_id' => $project->user_id,
        'project_count_type' => $project->project_count_type,
        'name' => $project->name,
        'description' => $project->description,
        'last_updated' => $timestamp,
        'date_created' => $timestamp
]]
->execute[];
0

Truy vấn đúng. Để sửa lại, hãy sử dụng hàm DATE như sau

$project = new stdClass[];
$project->user_id = get_user_id[];
$project->project_count_type = $form_state['values']['type'];
$project->name = $form_state['values']['name'];
$project->description = $form_state['values']['description'];

# get the current time in the proper format for a sql timestamp field
$timestamp = date['Y-m-d H:i:s'];

# new drupal 7 style insert
$id = db_insert['projects']
    ->fields[array[
        'user_id' => $project->user_id,
        'project_count_type' => $project->project_count_type,
        'name' => $project->name,
        'description' => $project->description,
        'last_updated' => $timestamp,
        'date_created' => $timestamp
]]
->execute[];
9

php




2019-10-02 18:41:17
23

2019-10-02 18:41:17
24

2019-10-02 18:41:17
25
$timestamp = date['Y-m-d H:i:s'];
30____131
$timestamp = date['Y-m-d H:i:s'];
32

$timestamp = date['Y-m-d H:i:s'];
33
$timestamp = date['Y-m-d H:i:s'];
30
$timestamp = date['Y-m-d H:i:s'];
35____132

$timestamp = date['Y-m-d H:i:s'];
37
$timestamp = date['Y-m-d H:i:s'];
30
$timestamp = date['Y-m-d H:i:s'];
39
$timestamp = date['Y-m-d H:i:s'];
32

2019-10-02 18:41:17
51
$timestamp = date['Y-m-d H:i:s'];
30
2019-10-02 18:41:17
53____132

2019-10-02 18:41:17
24

2019-10-02 18:41:17
56

2019-10-02 18:41:17
57
2019-10-02 18:41:17
58
2019-10-02 18:41:17
25
$timestamp = date['Y-m-d H:i:s'];
20
$timestamp = date['Y-m-d H:i:s'];
33
$timestamp = date['Y-m-d H:i:s'];
20
$timestamp = date['Y-m-d H:i:s'];
37
$timestamp = date['Y-m-d H:i:s'];
20
2019-10-02 18:41:17
51
$timestamp = date['Y-m-d H:i:s'];
26

2019-10-02 18:41:17
24

$timestamp = date['Y-m-d H:i:s'];
28

$timestamp = date['Y-m-d H:i:s'];
29
$timestamp = date['Y-m-d H:i:s'];
10
2019-10-02 18:41:17
57
$timestamp = date['Y-m-d H:i:s'];
12

$timestamp = date['Y-m-d H:i:s'];
13
$timestamp = date['Y-m-d H:i:s'];
14
$timestamp = date['Y-m-d H:i:s'];
15
$timestamp = date['Y-m-d H:i:s'];
16
$timestamp = date['Y-m-d H:i:s'];
17

$timestamp = date['Y-m-d H:i:s'];
18

2019-10-02 18:41:17
24

2019-10-02 18:41:17
509

$project = new stdClass[];
$project->user_id = get_user_id[];
$project->project_count_type = $form_state['values']['type'];
$project->name = $form_state['values']['name'];
$project->description = $form_state['values']['description'];

# get the current time in the proper format for a sql timestamp field
$timestamp = date['Y-m-d H:i:s'];

# new drupal 7 style insert
$id = db_insert['projects']
    ->fields[array[
        'user_id' => $project->user_id,
        'project_count_type' => $project->project_count_type,
        'name' => $project->name,
        'description' => $project->description,
        'last_updated' => $timestamp,
        'date_created' => $timestamp
]]
->execute[];
01
2019-10-02 18:41:17
511

2019-10-02 18:41:17
512
2019-10-02 18:41:17
513
2019-10-02 18:41:17
514
2019-10-02 18:41:17
515
2019-10-02 18:41:17
516

2019-10-02 18:41:17
24

2019-10-02 18:41:17
518
2019-10-02 18:41:17
519
2019-10-02 18:41:17
57
$timestamp = date['Y-m-d H:i:s'];
20
$project = new stdClass[];
$project->user_id = get_user_id[];
$project->project_count_type = $form_state['values']['type'];
$project->name = $form_state['values']['name'];
$project->description = $form_state['values']['description'];

# get the current time in the proper format for a sql timestamp field
$timestamp = date['Y-m-d H:i:s'];

# new drupal 7 style insert
$id = db_insert['projects']
    ->fields[array[
        'user_id' => $project->user_id,
        'project_count_type' => $project->project_count_type,
        'name' => $project->name,
        'description' => $project->description,
        'last_updated' => $timestamp,
        'date_created' => $timestamp
]]
->execute[];
01
2019-10-02 18:41:17
523

2019-10-02 18:41:17
24

$timestamp = date['Y-m-d H:i:s'];
29
$timestamp = date['Y-m-d H:i:s'];
15
2019-10-02 18:41:17
518
$timestamp = date['Y-m-d H:i:s'];
12

$timestamp = date['Y-m-d H:i:s'];
13
$project = new stdClass[];
$project->user_id = get_user_id[];
$project->project_count_type = $form_state['values']['type'];
$project->name = $form_state['values']['name'];
$project->description = $form_state['values']['description'];

# get the current time in the proper format for a sql timestamp field
$timestamp = date['Y-m-d H:i:s'];

# new drupal 7 style insert
$id = db_insert['projects']
    ->fields[array[
        'user_id' => $project->user_id,
        'project_count_type' => $project->project_count_type,
        'name' => $project->name,
        'description' => $project->description,
        'last_updated' => $timestamp,
        'date_created' => $timestamp
]]
->execute[];
96
2019-10-02 18:41:17
518
$timestamp = date['Y-m-d H:i:s'];
32
2019-10-02 18:41:17
533

2019-10-02 18:41:17
534

2019-10-02 18:41:17
230
2019-10-02 18:41:17
231

$timestamp = date['Y-m-d H:i:s'];
13
$project = new stdClass[];
$project->user_id = get_user_id[];
$project->project_count_type = $form_state['values']['type'];
$project->name = $form_state['values']['name'];
$project->description = $form_state['values']['description'];

# get the current time in the proper format for a sql timestamp field
$timestamp = date['Y-m-d H:i:s'];

# new drupal 7 style insert
$id = db_insert['projects']
    ->fields[array[
        'user_id' => $project->user_id,
        'project_count_type' => $project->project_count_type,
        'name' => $project->name,
        'description' => $project->description,
        'last_updated' => $timestamp,
        'date_created' => $timestamp
]]
->execute[];
96
$timestamp = date['Y-m-d H:i:s'];
351
$timestamp = date['Y-m-d H:i:s'];
352
$project = new stdClass[];
$project->user_id = get_user_id[];
$project->project_count_type = $form_state['values']['type'];
$project->name = $form_state['values']['name'];
$project->description = $form_state['values']['description'];

# get the current time in the proper format for a sql timestamp field
$timestamp = date['Y-m-d H:i:s'];

# new drupal 7 style insert
$id = db_insert['projects']
    ->fields[array[
        'user_id' => $project->user_id,
        'project_count_type' => $project->project_count_type,
        'name' => $project->name,
        'description' => $project->description,
        'last_updated' => $timestamp,
        'date_created' => $timestamp
]]
->execute[];
01
$timestamp = date['Y-m-d H:i:s'];
352
$timestamp = date['Y-m-d H:i:s'];
355
2019-10-02 18:41:17
235
2019-10-02 18:41:17
57
$timestamp = date['Y-m-d H:i:s'];
26

$timestamp = date['Y-m-d H:i:s'];
18

2019-10-02 18:41:17
24

2019-10-02 18:41:17
240

2019-10-02 18:41:17
241____257____326

2019-10-02 18:41:17
244


Đầu ra.

Để lấy năm, quý, tháng, tuần, ngày, giờ, phút và giây từ một giá trị DATETIME, hãy sử dụng các hàm như trong câu lệnh sau.
GIỜ[@dt], PHÚT[@dt], GIÂY[@dt], NGÀY[@dt], TUẦN[@dt], THÁNG[@dt], QUÝ[@dt]

PHP là ngôn ngữ kịch bản phía máy chủ được thiết kế dành riêng cho phát triển web. Bạn có thể học PHP từ đầu bằng cách làm theo Hướng dẫn PHP và Ví dụ về PHP này

Làm cách nào để chèn ngày tháng trong PHP?

Hàm PHP date_add[] $date=date_create["2013-03-15"]; date_add[$date,date_interval_create_from_date_string["40 days"]]; echo date_format[$date,"Y-m-d"];

Làm cách nào để chèn dấu thời gian trong PHP?

Khi bảng MySQL được tạo, chỉ cần làm điều này. chọn DẤU THỜI GIAN làm loại cột của bạn. đặt giá trị Mặc định thành CURRENT_TIMESTAMP. sau đó chỉ cần chèn bất kỳ hàng nào vào bảng mà không cần chèn bất kỳ giá trị nào cho cột thời gian

Làm cách nào để chèn ngày và giờ trong MySQL bằng PHP?

Phương pháp đơn giản nhất để chèn ngày giờ hiện tại vào MySQL là sử dụng hàm now[] . Khi bạn gọi hàm, nó sẽ trả về ngày và giờ hiện tại theo múi giờ được định cấu hình của hệ thống dưới dạng chuỗi. Giá trị được trả về từ hàm now[] là YYYY-MM-DD cho ngày và HH-MM-SS-UU cho bản ghi thời gian.

Làm cách nào để chèn ngày vào PHP trong MySQL?

Sau khi cơ sở dữ liệu và bảng đã được tạo, chúng ta có thể bắt đầu thêm dữ liệu vào đó. .
Truy vấn SQL phải được trích dẫn bằng PHP
Các giá trị chuỗi bên trong truy vấn SQL phải được trích dẫn
Các giá trị số không được trích dẫn
Từ NULL không được trích dẫn

Chủ Đề