Vòng lặp giữa hai ngày php

timedelta() được sử dụng để tính toán sự khác biệt về ngày tháng và cũng có thể được sử dụng để thao tác ngày tháng trong Python

Thí dụ

Nhưng khi sử dụng timedelta, chúng tôi không thể lặp lại các tháng giữa các ngày một cách hoàn hảo vì ở đây chúng tôi đang thêm 31 ngày cho mỗi tháng. Nhưng mỗi tháng sẽ không có đúng 31 ngày. Có tháng có 30 thậm chí 28, 29. Để giải quyết vấn đề, quy tắc đưa ra hành động giúp lặp lại giữa các ngày theo một khoảng thời gian cụ thể

Mã số

Python3




# import necessary packages

from datetimeimport datetime, timedelta

 

# date initialisation

rrule(rrule.MONTHLY, dtstart=start_date, until=end_date)
0
rrule(rrule.MONTHLY, dtstart=start_date, until=end_date)
1
rrule(rrule.MONTHLY, dtstart=start_date, until=end_date)
2
rrule(rrule.MONTHLY, dtstart=start_date, until=end_date)
3
rrule(rrule.MONTHLY, dtstart=start_date, until=end_date)
4
rrule(rrule.MONTHLY, dtstart=start_date, until=end_date)
5
rrule(rrule.MONTHLY, dtstart=start_date, until=end_date)
4
rrule(rrule.MONTHLY, dtstart=start_date, until=end_date)
7
rrule(rrule.MONTHLY, dtstart=start_date, until=end_date)
8

rrule(rrule.MONTHLY, dtstart=start_date, until=end_date)
9
rrule(rrule.MONTHLY, dtstart=start_date, until=end_date)
1
rrule(rrule.MONTHLY, dtstart=start_date, until=end_date)
2_______0_______3
rrule(rrule.MONTHLY, dtstart=start_date, until=end_date)
4
2021-01-01 00:00:00
2021-02-01 00:00:00
2021-03-01 00:00:00
2021-04-01 00:00:00
2021-05-01 00:00:00
2021-06-01 00:00:00
2021-07-01 00:00:00
2021-08-01 00:00:00
2021-09-01 00:00:00
2021-10-01 00:00:00
2021-11-01 00:00:00
2021-12-01 00:00:00
2022-01-01 00:00:00
4
rrule(rrule.MONTHLY, dtstart=start_date, until=end_date)
4
2021-01-01 00:00:00
2021-02-01 00:00:00
2021-03-01 00:00:00
2021-04-01 00:00:00
2021-05-01 00:00:00
2021-06-01 00:00:00
2021-07-01 00:00:00
2021-08-01 00:00:00
2021-09-01 00:00:00
2021-10-01 00:00:00
2021-11-01 00:00:00
2021-12-01 00:00:00
2022-01-01 00:00:00
6_______0_______8

 

2021-01-01 00:00:00
2021-02-01 00:00:00
2021-03-01 00:00:00
2021-04-01 00:00:00
2021-05-01 00:00:00
2021-06-01 00:00:00
2021-07-01 00:00:00
2021-08-01 00:00:00
2021-09-01 00:00:00
2021-10-01 00:00:00
2021-11-01 00:00:00
2021-12-01 00:00:00
2022-01-01 00:00:00
8

2021-01-01 00:00:00
2021-02-01 00:00:00
2021-03-01 00:00:00
2021-04-01 00:00:00
2021-05-01 00:00:00
2021-06-01 00:00:00
2021-07-01 00:00:00
2021-08-01 00:00:00
2021-09-01 00:00:00
2021-10-01 00:00:00
2021-11-01 00:00:00
2021-12-01 00:00:00
2022-01-01 00:00:00
9_______0_______1
2021-01-01 00:00:00
2022-01-01 00:00:00
1
rrule(rrule.MONTHLY, dtstart=start_date, until=end_date)
1
2021-01-01 00:00:00
2022-01-01 00:00:00
3
rrule(rrule.MONTHLY, dtstart=start_date, until=end_date)
8

 

2021-01-01 00:00:00
2022-01-01 00:00:00
5
2021-01-01 00:00:00
2022-01-01 00:00:00
6
rrule(rrule.MONTHLY, dtstart=start_date, until=end_date)
1
2021-01-01 00:00:00
2022-01-01 00:00:00
8

2021-01-01 00:00:00
2022-01-01 00:00:00
9_______136_______0# import necessary packages1

2021-01-01 00:00:00
2022-01-01 00:00:00
9____136_______3

2021-01-01 00:00:00
2022-01-01 00:00:00
9_______0_______0# import necessary packages6
rrule(rrule.MONTHLY, dtstart=start_date, until=end_date)
1
2021-01-01 00:00:00
2021-02-01 00:00:00
2021-03-01 00:00:00
2021-04-01 00:00:00
2021-05-01 00:00:00
2021-06-01 00:00:00
2021-07-01 00:00:00
2021-08-01 00:00:00
2021-09-01 00:00:00
2021-10-01 00:00:00
2021-11-01 00:00:00
2021-12-01 00:00:00
2022-01-01 00:00:00
9

Đầu ra

2020-01-10 00:00:00
2020-02-10 00:00:00
2020-03-12 00:00:00
2020-04-12 00:00:00

Phương pháp 2. cai trị

rrule là một gói có trong thư viện dateutil và gói này bao gồm một phương thức cũng rrule lấy dtstart, cho đến và khoảng thời gian cụ thể làm tham số là ngày bắt đầu, ngày kết thúc và khoảng thời gian dựa trên lần lặp tương ứng. Các khoảng thời gian cụ thể là HÀNG TUẦN, HÀNG THÁNG, HÀNG NĂM, v.v.

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_______146_______2  0

+2 years 3 months
1

 

    3

    5 0  7_______146_______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_______150_______4  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_______150_______4  5 // Creates DateTime objects0// Creates DateTime objects8

+2 years 3 months
05

+2 years 3 months
06_______150_______1
+2 years 3 months
08

 

  

+2 years 3 months
10

  

+2 years 3 months
12

  

+2 years 3 months
14

  37_______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 years 3 months
31

 

  

+2 years 3 months
33

  

+2 years 3 months
35

  

+2 years 3 months
37

  37_______39 0  0_______150_______4  5 // Creates DateTime objects0// Creates DateTime objects8

+2 years 3 months
23

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

+2 years 3 months
53_______148_______0
+2 years 3 months
16
+2 years 3 months
56

 

  

+2 years 3 months
33

  

+2 years 3 months
60

  37_______62 0  0$datetime14  5 // Creates DateTime objects0// Creates DateTime objects8

+2 years 3 months
23

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

+2 years 3 months
76_______148_______0
+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_______37_______86

+2 years 3 months
87_______37_______88
+2 years 3 months
89_______43_______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_______151_______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_______37_______62
+2 years 3 months
1

Difference between two dates: 103
4

đầu ra

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

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 years 3 months
1

  

2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
16 01_______146_______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
2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
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 để lặp lại giữa hai ngày trong php?

Vòng lặp PHP giữa hai ngày với tên ngày. php date_default_timezone_set('UTC');

Làm cách nào để tính ngày giữa hai ngày trong php?

Hàm date_diff() là một hàm có sẵn trong PHP được sử dụng để tính toán sự khác biệt giữa hai ngày. Hàm này trả về một đối tượng DateInterval nếu thành công và trả về FALSE nếu thất bại.

Làm cách nào để có được tất cả các ngày giữa hai ngày trong laravel?

Chúng ta sẽ cần dùng đến DB. raw() để sao chép cái này với Eloquent, trông sẽ như thế này. $startDate = '2022-06-01'; . whereBetween(DB. raw('DATE(created_at)'), [$startDate, $endDate])->get();

Chúng ta có thể sử dụng hai vòng lặp foreach trong php không?

Đầu ra. Lưu ý − Nếu có nhiều hơn 2 mảng, thì có thể sử dụng các vòng lặp 'foreach' lồng nhau .