Php sql giữa hai ngày

Cơ sở dữ liệu của chúng tôi có một bảng tên là food với dữ liệu trong các cột id, name, purchase_dateexpiration_date

idnamepurchase_dateexpiration_date1bread2019-07-202019-08-222bơ2018-07-302019-08-103sữa2019-01-122019-01-134sữa chua2019-02-252019-02-24

Đối với mỗi sản phẩm thực phẩm, hãy lấy tên của sản phẩm và số ngày giữa ngày hết hạn và ngày mua

Giải pháp

Chúng ta sẽ sử dụng hàm DATEDIFF(). Đây là truy vấn bạn sẽ viết

SELECT
  name,
  DATEDIFF(expiration_date, purchase_date) AS days
FROM food;

Đây là kết quả của truy vấn

tên bánh mì ngày33bơ376sữa1sữa chua-1

Thảo luận

Sử dụng hàm DATEDIFF() để lấy số ngày giữa hai ngày trong cơ sở dữ liệu MySQL. Hàm này nhận hai đối số

  1. ngày kết thúc. (Trong ví dụ của chúng tôi, đó là cột expiration_date. )
  2. Ngày bắt đầu. (Trong ví dụ của chúng tôi, đó là cột purchase_date. )

Các đối số này có thể là giá trị ngày/ngày giờ, biểu thức trả về giá trị ngày/ngày giờ hoặc cột của kiểu dữ liệu ngày giờ hoặc ngày tháng

Hàm này trừ ngày bắt đầu từ ngày kết thúc và trả về số ngày dưới dạng số nguyên. Trong ví dụ của chúng tôi, bơ đã được mua vào '2018-07-30', nhưng ngày hết hạn của nó là '2019-08-10'; . Lưu ý rằng sữa chua đã được mua như một sản phẩm lỗi thời. chênh lệch số ngày là -1 và ngày mua của nó muộn hơn ngày hết hạn

Trong bài viết này, chúng ta sẽ xem cách tính chênh lệch giữa 2 ngày trong PHP, cùng với việc hiểu triển khai của nó thông qua các ví dụ. Đưa ra hai ngày tức là. , start_date và end_date & chúng ta cần tìm sự khác biệt giữa hai ngày

Xem xét ví dụ dưới đây

Input: start_date: 2016-06-01 22:45:00 
       end_date: 2018-09-21 10:44:01
Output: 2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
Explanation: The difference of 2 dates will give the date in complete format.

Phương pháp 1. Sử dụng hàm date_diff()

Hàm này được sử dụng để tìm sự khác biệt giữa hai ngày. Hàm này sẽ trả về một đối tượng DateInterval nếu thành công và trả về FALSE nếu thất bại

Thí dụ. Ví dụ này minh họa việc sử dụng hàm date_diff() để tính chênh lệch giữa 2 ngày

PHP




 

  // Creates DateTime objects

  $datetime1 = date_create(

+2 years 3 months
0
+2 years 3 months
1

  

+2 years 3 months
3 = date_create(
+2 years 3 months
5
+2 years 3 months
1

 

  

+2 years 3 months
8

  

2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
0
2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
1$datetime1
2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
3
+2 years 3 months
3
+2 years 3 months
1

 

  

2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
7

  

2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
9
2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
0
Difference between two dates: 103
1
Difference between two dates: 103
2
+2 years 3 months
1

Difference between two dates: 103
4

đầu ra

+2 years 3 months

Phương pháp 2. Để sử dụng công thức toán học ngày-giờ để tìm sự khác biệt giữa hai ngày. Nó trả về năm, tháng, ngày, giờ, phút, giây giữa hai ngày được chỉ định

Thí dụ. Trong ví dụ này, chúng ta sẽ sử dụng công thức toán học ngày-thời gian để tính chênh lệch giữa các ngày sẽ được trả về theo năm, tháng, ngày, giờ, phút, giây

PHP




 

  

Difference between two dates: 103
7

  

Difference between two dates: 103
9 0123
+2 years 3 months
1

  6 01_______110_______2  0

+2 years 3 months
1

 

    3

    5 0  7_______110_______26 // Creates DateTime objects0

Difference between two dates: 103
9
+2 years 3 months
1

 

  // Creates DateTime objects4

  // Creates DateTime objects6

  // Creates DateTime objects8 0  02  5   3

 

    5

    7

    9

  $datetime11 0  0$datetime14  5 // Creates DateTime objects0// Creates DateTime objects8 $datetime18

$datetime19= date_create(0

 

  = date_create(2

  = date_create(4

  = date_create(6

  = date_create(8 0  0$datetime14  5 // Creates DateTime objects0// Creates DateTime objects8

+2 years 3 months
05

+2 years 3 months
06$datetime11
+2 years 3 months
08

 

  

+2 years 3 months
10

  

+2 years 3 months
12

  

+2 years 3 months
14

  2_______16 0  0$datetime14  5 // Creates DateTime objects0// Creates DateTime objects8

+2 years 3 months
23

+2 years 3 months
24// Creates DateTime objects0$datetime11
+2 years 3 months
27= date_create(8
+2 years 3 months
29

+2 years 3 months
30_______2_______31

 

  

+2 years 3 months
33

  

+2 years 3 months
35

  

+2 years 3 months
37

  ___

+2 years 3 months
39 0  0$datetime14  5 // Creates DateTime objects0// Creates DateTime objects8
+2 years 3 months
23

+2 years 3 months
47_______112_______0$datetime11
+2 years 3 months
27= date_create(8
+2 years 3 months
52

+2 years 3 months
53// Creates DateTime objects0
+2 years 3 months
16
+2 years 3 months
56

 

  

+2 years 3 months
33

  

+2 years 3 months
60

  ___

+2 years 3 months
62 0  0$datetime14  5 // Creates DateTime objects0// Creates DateTime objects8
+2 years 3 months
23

+2 years 3 months
47_______112_______0$datetime11
+2 years 3 months
27= date_create(8
+2 years 3 months
52

+2 years 3 months
76// Creates DateTime objects0
+2 years 3 months
16
+2 years 3 months
79
+2 years 3 months
39
+2 years 3 months
81

 

  

+2 years 3 months
83

  

+2 years 3 months
85_______2_______86

+2 years 3 months
87
+2 years 3 months
88
+2 years 3 months
89
2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
3// Creates DateTime objects8
2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
3$datetime11
2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
3

+2 years 3 months
06= date_create(8
2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
3
+2 years 3 months
16
2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
3
+2 years 3 months
39
2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
3
+2 years 3 months
62
+2 years 3 months
1

Difference between two dates: 103
4

đầu ra

________số 8_______

Phương pháp 3. Phương pháp này được sử dụng để lấy tổng số ngày giữa hai ngày được chỉ định

PHP




 

  

2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
07

  

2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
09 012
2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
13_______2_______1

  ___

2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
16 01_______110_______2
2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
20
+2 years 3 months
1

 

  

2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
23

  

2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
25

  

2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
27

  

2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
9
2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
30

2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
31_______8_______32
2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
16 // Creates DateTime objects0
2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
09
2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
36

Difference between two dates: 103
4

đầu ra

Difference between two dates: 103

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 để kiểm tra giữa hai ngày trong php?

hàm php displayDates($date1, $date2, $format = 'd-m-Y' ) { $dates = array();

Làm cách nào để lấy dữ liệu giữa hai ngày trong SQL?

Như đã nêu ở trên, định dạng ngày và giờ trong bảng của chúng ta sẽ là yyyy. mm. đh. mm. ss được ngụ ý bởi DATETIME2. Thời gian ở định dạng 24 giờ. cú pháp. CHỌN * TỪ TABLE_NAME WHERE DATE_TIME_COLUMN GIỮA 'STARTING_DATE_TIME' VÀ 'ENDING_DATE_TIME';

Làm cách nào để so sánh hai ngày trong php MySQL?

chúng ta có thể phân tích ngày bằng toán tử so sánh đơn giản nếu ngày đã cho có định dạng tương tự. ? php $date1 = "24-11-2018";

Làm cách nào để tìm giữa hai ngày trong MySQL?

Để đếm sự khác biệt giữa các ngày trong MySQL, hãy sử dụng hàm DATEDIFF(ngày kết thúc, ngày bắt đầu) . Sự khác biệt giữa ngày bắt đầu và ngày kết thúc được biểu thị bằng ngày. Trong trường hợp này, ngày kết thúc là ngày đến và ngày bắt đầu là ngày khởi hành.