Laravel tính chênh lệch ngày như thế nào?

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




<?php

 

  // Creates DateTime objects

  $datetime1 = date_create(+2 years 3 months0+2 years 3 months1

  +2 years 3 months3 = date_create(+2 years 3 months5+2 years 3 months1

 

  +2 years 3 months8

  7_______0 2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds1$datetime12 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds3+2 years 3 months3+2 years 3 months1

 

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

  2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds9 2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds0Difference between two dates: 1031Difference between two dates: 1032+2 years 3 months1

Difference between two dates: 1034

đầ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




<?php

 

  Difference between two dates: 1037

  15_______9 <?php0<?php1<?php2<?php3+2 years 3 months1

  100_______6 <?php0<?php1<?php2  0+2 years 3 months1

 

    3

  101_______5 <?php0  7_______100_______2<?php6 // Creates DateTime objects0Difference between two dates: 1039_______1_______1

 

  // Creates DateTime objects4

  // Creates DateTime objects6

  102_______8 <?php0  0_______100_______2  5   3

 

    5

    7

    9

  104_______1 <?php0  0$datetime14  5 // Creates DateTime objects0// Creates DateTime objects8 $datetime18

$datetime19= date_create(0

 

  = date_create(2

  = date_create(4

________ 101 _______________ 105 _______ 6

  105_______8 <?php0  0$datetime14  5 // Creates DateTime objects0// Creates DateTime objects8 +2 years 3 months05

+2 years 3 months06$datetime11+2 years 3 months08

 

  +2 years 3 months10

  +2 years 3 months12

  +2 years 3 months14

  1_______16 <?php0  0$datetime14  5 // Creates DateTime objects0// Creates DateTime objects8 +2 years 3 months23

+2 years 3 months24// Creates DateTime objects0$datetime11+2 years 3 months27= date_create(8+2 years 3 months29

+2 years 3 months30_______1_______31

 

  +2 years 3 months33

  +2 years 3 months35

  +2 years 3 months37

  1_______39 <?php0  0$datetime14  5 // Creates DateTime objects0// Creates DateTime objects8 +2 years 3 months23

+2 years 3 months47_______102_______0$datetime11+2 years 3 months27= date_create(8+2 years 3 months52

+2 years 3 months53// Creates DateTime objects0+2 years 3 months16+2 years 3 months56

 

  +2 years 3 months33

  +2 years 3 months60

  1_______62 <?php0  0$datetime14  5 // Creates DateTime objects0// Creates DateTime objects8 +2 years 3 months23

+2 years 3 months47_______102_______0$datetime11+2 years 3 months27= date_create(8+2 years 3 months52

+2 years 3 months76// Creates DateTime objects0+2 years 3 months16+2 years 3 months79+2 years 3 months39+2 years 3 months81

 

  +2 years 3 months83

  +2 years 3 months85_______1_______86

+2 years 3 months87+2 years 3 months88+2 years 3 months892 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds3// Creates DateTime objects82 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds3$datetime112 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds3

+2 years 3 months06= date_create(82 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds3+2 years 3 months162 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds3+2 years 3 months392 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds3+2 years 3 months62+2 years 3 months1

Difference between two dates: 1034

đầ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




<?php

 

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

  7_______09 <?php0<?php1<?php22 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds13+2 years 3 months1

  7_______16 <?php0<?php1<?php22 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds20+2 years 3 months1

 

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

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

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

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

2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds312 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds322 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds16 // Creates DateTime objects02 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds092 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds36

Difference between two dates: 1034

đầ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 để trừ ngày tháng trong laravel?

Bạn có thể trừ ngày vào ngày hiện tại bằng cách sử dụng carbon trong phiên bản laravel 6, laravel 7, laravel 8 và laravel 9. Nếu bạn cần trừ ngày hoặc nhiều ngày hơn thì bạn có thể sử dụng carbon trong laravel. carbon cung cấp phương thức sunDay() và sunDays() để thêm ngày vào đối tượng ngày carbon.

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

Laravel 8. Nhận dữ liệu giữa hai ngày .
Bước 1. Cài đặt Laravel
Bước 2. Cơ sở dữ liệu thiết lập
Bước 3. Tạo mô hình và di chuyển
Bước 4. Tạo dữ liệu giả
Bước 5. Xác định tuyến đường & logic
Bước 6. Chế độ xem thiết lập
Bước 7. thử nghiệm

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

hàm php week_between_two_dates($date1, $date2) { $first = DateTime. createFromFormat('m/d/Y', $date1); . createFromFormat('m/d/Y', $date2);

Chủ đề