Hướng dẫn php between two dates - php giữa hai ngày

TL; DR không sử dụng dấu thời gian UNIX. Không sử dụng time[]. Nếu bạn làm như vậy, hãy chuẩn bị nếu độ tin cậy 98.0825% của nó không thành công. Sử dụng datetime [hoặc carbon]. do not use UNIX timestamps. Do not use time[]. If you do, be prepared should its 98.0825% reliability fail you. Use DateTime [or Carbon].

Câu trả lời đúng là câu trả lời được đưa ra bởi Saksham Gupta [các câu trả lời khác cũng đúng]:correct answer is the one given by Saksham Gupta [other answers are also correct]:correct answer is the one given by Saksham Gupta [other answers are also correct]:

Nội dung chính

  • TL; DR không sử dụng dấu thời gian UNIX. Không sử dụng time[]. Nếu bạn làm như vậy, hãy chuẩn bị nếu độ tin cậy 98.0825% của nó không thành công. Sử dụng datetime [hoặc carbon]. do not use UNIX timestamps. Do not use time[]. If you do, be prepared should its 98.0825% reliability fail you. Use DateTime [or Carbon].
  • Câu trả lời dài: Tại sao chia cho 24*60*60 [còn gọi là 86400] không an toàn
  • Dù giải pháp của bạn là gì, hãy kiểm tra nó!
  • Câu chuyện kinh dị: Chi phí của "Tiết kiệm thời gian"
  • Làm thế nào tôi có thể nhận được ngày giữa hai ngày trong PHP?
  • Làm thế nào để bạn tính toán ngày giữa hai ngày?
  • Làm thế nào có thể đếm ngày trong PHP?
  • Làm thế nào để bạn kiểm tra xem một ngày là giữa hai ngày trong PHP?

$date1 = new DateTime['2010-07-06'];
$date2 = new DateTime['2010-07-09'];
$days  = $date2->diff[$date1]->format['%a'];

Hoặc theo thủ tục là một lớp lót:

/**
 * Number of days between two dates.
 *
 * @param date $dt1    First date
 * @param date $dt2    Second date
 * @return int
 */
function daysBetween[$dt1, $dt2] {
    return date_diff[
        date_create[$dt2],  
        date_create[$dt1]
    ]->format['%a'];
}

Với một cảnh báo: '%A' dường như chỉ ra số ngày tuyệt đối. Nếu bạn muốn nó là một số nguyên đã ký, tức là âm khi ngày thứ hai là trước lần thứ nhất, thì bạn cần sử dụng tiền tố '%r' [tức là

/**
 * Number of days between two dates.
 *
 * @param date $dt1    First date
 * @param date $dt2    Second date
 * @return int
 */
function daysBetween[$dt1, $dt2] {
    return date_diff[
        date_create[$dt2],  
        date_create[$dt1]
    ]->format['%a'];
}
0].absolute number of days. If you want it as a signed integer, i.e. negative when the second date is before the first, then you need to use the '%r' prefix [i.e.
/**
 * Number of days between two dates.
 *
 * @param date $dt1    First date
 * @param date $dt2    Second date
 * @return int
 */
function daysBetween[$dt1, $dt2] {
    return date_diff[
        date_create[$dt2],  
        date_create[$dt1]
    ]->format['%a'];
}
0].absolute number of days. If you want it as a signed integer, i.e. negative when the second date is before the first, then you need to use the '%r' prefix [i.e.
/**
 * Number of days between two dates.
 *
 * @param date $dt1    First date
 * @param date $dt2    Second date
 * @return int
 */
function daysBetween[$dt1, $dt2] {
    return date_diff[
        date_create[$dt2],  
        date_create[$dt1]
    ]->format['%a'];
}
0].

Nếu bạn thực sự phải sử dụng dấu thời gian UNIX, hãy đặt múi giờ thành GMT để tránh hầu hết các cạm bẫy chi tiết dưới đây.set the time zone to GMT to avoid most of the pitfalls detailed below.set the time zone to GMT to avoid most of the pitfalls detailed below.

Câu trả lời dài: Tại sao chia cho 24*60*60 [còn gọi là 86400] không an toàn

Dù giải pháp của bạn là gì, hãy kiểm tra nó!wrong results and subtle bugs that may be difficult to track, and arise even days, weeks or months after a successful deployment. It's not that the solution doesn't work - it works. Today. But it might stop working tomorrow.

Câu chuyện kinh dị: Chi phí của "Tiết kiệm thời gian"zero if between the present and the instant indicated by "yesterday" less than one whole day has passed.

Làm thế nào tôi có thể nhận được ngày giữa hai ngày trong PHP?midnight of that particular day.

Làm thế nào để bạn tính toán ngày giữa hai ngày?will report one day less than expected.

Làm thế nào có thể đếm ngày trong PHP?wrong answer.

Làm thế nào để bạn kiểm tra xem một ngày là giữa hai ngày trong PHP?almost always true - it happens often enough to overlook the times it doesn't. But the distance in seconds between two consecutive midnights is surely not 86400 at least twice a year when daylight saving time comes into play. Comparing two dates across a DST boundary will yield the wrong answer.

Hoặc theo thủ tục là một lớp lót:

 FLOOR[[unix_timestamp[DATE2] - unix_timestamp[DATE1]] / 86400]

Với một cảnh báo: '%A' dường như chỉ ra số ngày tuyệt đối. Nếu bạn muốn nó là một số nguyên đã ký, tức là âm khi ngày thứ hai là trước lần thứ nhất, thì bạn cần sử dụng tiền tố '%r' [tức là

/**
 * Number of days between two dates.
 *
 * @param date $dt1    First date
 * @param date $dt2    Second date
 * @return int
 */
function daysBetween[$dt1, $dt2] {
    return date_diff[
        date_create[$dt2],  
        date_create[$dt1]
    ]->format['%a'];
}
0].absolute number of days. If you want it as a signed integer, i.e. negative when the second date is before the first, then you need to use the '%r' prefix [i.e.
/**
 * Number of days between two dates.
 *
 * @param date $dt1    First date
 * @param date $dt2    Second date
 * @return int
 */
function daysBetween[$dt1, $dt2] {
    return date_diff[
        date_create[$dt2],  
        date_create[$dt1]
    ]->format['%a'];
}
0].

 floor[[time[] - strtotime[$somedate]] / 86400]

Nếu bạn thực sự phải sử dụng dấu thời gian UNIX, hãy đặt múi giờ thành GMT để tránh hầu hết các cạm bẫy chi tiết dưới đây.set the time zone to GMT to avoid most of the pitfalls detailed below.

Và mọi thứ phát triển thậm chí còn xấu hơn vì mã như vậy không thể di động trên các nền tảng, bởi vì một số trong số chúng có thể áp dụng các giây nhảy và một số có thể không. Trên các nền tảng đó, sự khác biệt giữa hai ngày sẽ không phải là 86400 mà là 86401 hoặc có thể 86399. Vì vậy, mã hoạt động trong tháng 5 và thực sự đã vượt qua tất cả các bài kiểm tra sẽ bị phá vỡ vào tháng 6 tới khi 12.99999 ngày được coi là 12 ngày thay vì 13. Hai ngày Điều đó đã hoạt động trong năm 2015 sẽ không hoạt động trong năm 2017 - cùng ngày và cả năm không phải là một năm bước nhảy vọt. Và giữa năm 2018-03-01 và 2017-03-01, trên những nền tảng chăm sóc, 366 ngày sẽ trôi qua thay vì 365, biến năm 2018 trở thành một năm bước nhảy vọt [không phải vậy].is not portable across platforms, because some of them may apply leap seconds and some might not. On those platforms that do, the difference between two dates will not be 86400 but 86401, or maybe 86399. So code that worked in May and actually passed all tests will break next June when 12.99999 days are considered 12 days instead of 13. Two dates that worked in 2015 will not work in 2017 -- the same dates, and neither year is a leap year. And between 2018-03-01 and 2017-03-01, on those platforms that care, 366 days will have passed instead of 365, making 2018 a leap year [which it is not].is not portable across platforms, because some of them may apply leap seconds and some might not. On those platforms that do, the difference between two dates will not be 86400 but 86401, or maybe 86399. So code that worked in May and actually passed all tests will break next June when 12.99999 days are considered 12 days instead of 13. Two dates that worked in 2015 will not work in 2017 -- the same dates, and neither year is a leap year. And between 2018-03-01 and 2017-03-01, on those platforms that care, 366 days will have passed instead of 365, making 2018 a leap year [which it is not].

Vì vậy, nếu bạn thực sự muốn sử dụng dấu thời gian Unix:

  • Sử dụng hàm

    /**
     * Number of days between two dates.
     *
     * @param date $dt1    First date
     * @param date $dt2    Second date
     * @return int
     */
    function daysBetween[$dt1, $dt2] {
        return date_diff[
            date_create[$dt2],  
            date_create[$dt1]
        ]->format['%a'];
    }
    
    0 một cách khôn ngoan, không phải
    /**
     * Number of days between two dates.
     *
     * @param date $dt1    First date
     * @param date $dt2    Second date
     * @return int
     */
    function daysBetween[$dt1, $dt2] {
        return date_diff[
            date_create[$dt2],  
            date_create[$dt1]
        ]->format['%a'];
    }
    
    2.
  • Thay vào đó, không tính toán sự khác biệt giữa D1-M1-YYY1 và D2-M2-YYY2. Những ngày đó sẽ thực sự được coi là D1-M1-YYY1 00:00:00 và D2-M2-YYY2 00:00:00. Thay vào đó, chuyển đổi giữa D1-M1-YYY1 22:30:00 và D2-M2-YYY2 04:30:00. Bạn sẽ luôn nhận được một phần còn lại khoảng hai mươi giờ. Điều này có thể trở thành hai mươi mốt giờ hoặc mười chín, và có thể mười tám giờ, năm mươi chín phút ba mươi sáu giây. Không vấn đề. Đó là một biên độ lớn sẽ ở lại đó và sống tích cực cho tương lai gần. Bây giờ bạn có thể cắt ngắn nó với

    /**
     * Number of days between two dates.
     *
     * @param date $dt1    First date
     * @param date $dt2    Second date
     * @return int
     */
    function daysBetween[$dt1, $dt2] {
        return date_diff[
            date_create[$dt2],  
            date_create[$dt1]
        ]->format['%a'];
    }
    
    2 về an toàn.

Mặc dù vậy, giải pháp chính xác, để tránh các hằng số ma thuật, làm tròn Kludges và một khoản nợ bảo trì, làcorrect solution though, to avoid magic constants, rounding kludges and a maintenance debt, is tocorrect solution though, to avoid magic constants, rounding kludges and a maintenance debt, is to

  • sử dụng thư viện thời gian [DateTime, carbon, bất cứ điều gì]; Đừng tự lăn

  • Viết các trường hợp thử nghiệm toàn diện bằng cách sử dụng các lựa chọn ngày thực sự xấu xa - qua các ranh giới DST, qua những năm bước nhảy, trên những giây bước nhảy, v.v., cũng như ngày phổ biến. Lý tưởng nhất [các cuộc gọi đến DateTime là nhanh chóng!] Tạo ra bốn năm [và một ngày] có giá trị ngày bằng cách lắp ráp chúng từ các chuỗi, tuần tự và đảm bảo rằng sự khác biệt giữa ngày đầu tiên được kiểm tra tăng đều đặn. Điều này sẽ đảm bảo rằng nếu bất cứ điều gì thay đổi trong các thói quen cấp thấp và sửa chữa Seconds, hãy cố gắng tàn phá, ít nhất bạn sẽ biết.comprehensive test cases using really evil date choices - across DST boundaries, across leap years, across leap seconds, and so on, as well as commonplace dates. Ideally [calls to datetime are fast!] generate four whole years' [and one day] worth of dates by assembling them from strings, sequentially, and ensure that the difference between the first day and the day being tested increases steadily by one. This will ensure that if anything changes in the low-level routines and leap seconds fixes try to wreak havoc, at least you will know.comprehensive test cases using really evil date choices - across DST boundaries, across leap years, across leap seconds, and so on, as well as commonplace dates. Ideally [calls to datetime are fast!] generate four whole years' [and one day] worth of dates by assembling them from strings, sequentially, and ensure that the difference between the first day and the day being tested increases steadily by one. This will ensure that if anything changes in the low-level routines and leap seconds fixes try to wreak havoc, at least you will know.

  • Chạy các bài kiểm tra thường xuyên cùng với phần còn lại của bộ thử nghiệm. Chúng là một vấn đề của mili giây và có thể giúp bạn tiết kiệm hàng giờ đầu.

Dù giải pháp của bạn là gì, hãy kiểm tra nó!

Hàm

/**
 * Number of days between two dates.
 *
 * @param date $dt1    First date
 * @param date $dt2    Second date
 * @return int
 */
function daysBetween[$dt1, $dt2] {
    return date_diff[
        date_create[$dt2],  
        date_create[$dt1]
    ]->format['%a'];
}
3 bên dưới thực hiện một trong các giải pháp [như nó xảy ra, một giải pháp được chấp nhận] trong một kịch bản thế giới thực.

Bài Viết Liên Quan

Chủ Đề