Hướng dẫn php dateinterval format - định dạng ngày tháng trong php
❮ Tham khảo ngày/giờ PHP Show Thí dụTính khoảng thời gian giữa hai ngày, sau đó định dạng khoảng thời gian: $date1=date_create("2013-01-01"); // %A đầu ra Tổng số ngày của Daysecho $ Diff-> ("Tổng số ngày: %a.");?> Hãy tự mình thử » Định nghĩa và cách sử dụngHàm date_interval_format () là bí danh của dateInterval :: định dạng (). Hàm dateInterVal :: định dạng () được sử dụng để định dạng khoảng. & Nbsp; Cú phápDateInterval :: Định dạng (Định dạng) Giá trị tham số
Chi tiết kỹ thuật
❮ Tham khảo ngày/giờ PHP Tôi đang sử dụng mã này:
Tôi muốn tính toán, nếu ai đó bị trễ trả thì họ sẽ bị phạt mỗi tháng. Và thông báo lỗi là:
Đã hỏi ngày 25 tháng 6 năm 2016 lúc 13:44Jun 25, 2016 at 13:44 2 Thông báo lỗi xuất hiện vì 0 không phải là INT, mà là một đối tượng DateTime như thế này:
Bạn có thể nhận được giá trị số nguyên của những tháng như thế này
Kiểm tra kết quả trên trong hộp cát PHP Vì vậy, về cơ bản mã của bạn sẽ trông giống như
Đã trả lời ngày 25 tháng 6 năm 2016 lúc 13:51Jun 25, 2016 at 13:51 1 Bạn tính toán sự khác biệt trong tháng, nhưng bạn không bao giờ thực sự sử dụng giá trị đó. Phương thức 1 đưa ra một cái gì đó, nhưng không thay đổi ngày thực tế.Hãy thử nó như thế này:
Đã trả lời ngày 25 tháng 6 năm 2016 lúc 13:48Jun 25, 2016 at 13:48 PevarapevaraPevara 14.1k1 Huy hiệu vàng32 Huy hiệu bạc47 Huy hiệu đồng1 gold badge32 silver badges47 bronze badges 3 Câu trả lời của Peter Darmis là sai. Tôi không thể xuống cũng như bình luận vì vậy tôi sẽ thêm câu trả lời mới này. DateInterval đại diện cho một khoảng thời gian bằng cách giải mã khoảng thời gian ở các phần khác nhau, năm, tháng, ngày, v.v ... vì vậy "M" trong giải pháp được đề xuất sẽ không bao giờ lớn hơn 12. Vì vậy, có lẽ bạn nên làm điều gì đó như thế này:
Kiểm tra nó trong hộp cát: http://sandbox.onlinephpfunces.com/code/907b39ffee4586c4c9481ff3e0ea1aeb2d27c7b8 Đã trả lời ngày 13 tháng 9 năm 2019 lúc 14:35Sep 13, 2019 at 14:35 Iblancoblancoiblanco 411 Huy hiệu bạc6 Huy hiệu đồng1 silver badge6 bronze badges (Php 5> = 5.3.0, Php 7, Php 8) DateInterval :: __ Xây dựng - Tạo một đối tượng DateInterval mới — Creates a new DateInterval object Sự mô tảđịnh dạng DateInterval::__construct(string Yêu cầu. Chỉ định định dạng. Các ký tự sau có thể được sử dụng trong chuỗi tham số định dạng:% - % theo nghĩa đenY - năm, ít nhất 2 chữ số với số 0 hàng đầu (ví dụ 03) y - năm (ví dụ 3) M - Tháng, với số 0 hàng đầu (ví dụ 06)
M - Tháng (ví dụ 6)
A - Tổng số ngày là kết quả của Date_Diff () H - Giờ, với số 0 hàng đầu (ví dụ: 08, 23) Errors/ExceptionsH - giờ (ví dụ 8, 23)Exception when the 3 cannot be parsed as an interval. I - phút, với số 0 hàng đầu (ví dụ: 08, 23)
% - % theo nghĩa đenY - năm, ít nhất 2 chữ số với số 0 hàng đầu (ví dụ 03)DateInterval objects 0y - năm (ví dụ 3) M - Tháng, với số 0 hàng đầu (ví dụ 06)DateInterval example 1 2 3Đầu ra của ví dụ trên trong Php 8.2: object(DateInterval)#1 (10) { ["y"]=> int(0) ["m"]=> int(0) ["d"]=> int(9) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["f"]=> float(0) ["invert"]=> int(0) ["days"]=> bool(false) ["from_string"]=> bool(false) } Đầu ra của ví dụ trên trong Php 8: object(DateInterval)#1 (16) { ["y"]=> int(0) ["m"]=> int(0) ["d"]=> int(9) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["f"]=> float(0) ["weekday"]=> int(0) ["weekday_behavior"]=> int(0) ["first_last_day_of"]=> int(0) ["invert"]=> int(0) ["days"]=> bool(false) ["special_type"]=> int(0) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(0) ["have_special_relative"]=> int(0) } Đầu ra của ví dụ trên trong Php 7: object(DateInterval)#1 (16) { ["y"]=> int(0) ["m"]=> int(0) ["d"]=> int(2) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["f"]=> float(0) ["weekday"]=> int(0) ["weekday_behavior"]=> int(0) ["first_last_day_of"]=> int(0) ["invert"]=> int(0) ["days"]=> bool(false) ["special_type"]=> int(0) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(0) ["have_special_relative"]=> int(0) } Xem thêm
Owen tại niềm tin.com ¶ 9 năm trước 4 5 6 7 8 9 0 1Buvinghausen tại Gmail Dot Com ¶ ¶ 10 năm trước 2 3 1Hernanibus ¶ ¶ 4 năm trước 5 6 7 8 9 0 1Kevinpeno tại Gmail Dot Com ¶ ¶ 11 năm trước 2 3 1Daniellehr tại gmx dot de ¶ ¶ 10 năm trước 5 6 1Hernanibus ¶ ¶ 11 năm trước 8 9 1 10 năm trước object(DateInterval)#1 (10) { ["y"]=> int(0) ["m"]=> int(0) ["d"]=> int(9) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["f"]=> float(0) ["invert"]=> int(0) ["days"]=> bool(false) ["from_string"]=> bool(false) }1 object(DateInterval)#1 (10) { ["y"]=> int(0) ["m"]=> int(0) ["d"]=> int(9) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["f"]=> float(0) ["invert"]=> int(0) ["days"]=> bool(false) ["from_string"]=> bool(false) }2 1Hernanibus ¶ ¶ 4 năm trước object(DateInterval)#1 (10) { ["y"]=> int(0) ["m"]=> int(0) ["d"]=> int(9) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["f"]=> float(0) ["invert"]=> int(0) ["days"]=> bool(false) ["from_string"]=> bool(false) }4 object(DateInterval)#1 (10) { ["y"]=> int(0) ["m"]=> int(0) ["d"]=> int(9) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["f"]=> float(0) ["invert"]=> int(0) ["days"]=> bool(false) ["from_string"]=> bool(false) }5 object(DateInterval)#1 (10) { ["y"]=> int(0) ["m"]=> int(0) ["d"]=> int(9) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["f"]=> float(0) ["invert"]=> int(0) ["days"]=> bool(false) ["from_string"]=> bool(false) }6Kevinpeno tại Gmail Dot Com ¶ 11 năm trước ¶
object(DateInterval)#1 (10) { ["y"]=> int(0) ["m"]=> int(0) ["d"]=> int(9) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["f"]=> float(0) ["invert"]=> int(0) ["days"]=> bool(false) ["from_string"]=> bool(false) }8 object(DateInterval)#1 (10) { ["y"]=> int(0) ["m"]=> int(0) ["d"]=> int(9) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["f"]=> float(0) ["invert"]=> int(0) ["days"]=> bool(false) ["from_string"]=> bool(false) }9 1Daniellehr tại gmx dot de ¶ ¶
object(DateInterval)#1 (16) { ["y"]=> int(0) ["m"]=> int(0) ["d"]=> int(9) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["f"]=> float(0) ["weekday"]=> int(0) ["weekday_behavior"]=> int(0) ["first_last_day_of"]=> int(0) ["invert"]=> int(0) ["days"]=> bool(false) ["special_type"]=> int(0) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(0) ["have_special_relative"]=> int(0) }1 object(DateInterval)#1 (16) { ["y"]=> int(0) ["m"]=> int(0) ["d"]=> int(9) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["f"]=> float(0) ["weekday"]=> int(0) ["weekday_behavior"]=> int(0) ["first_last_day_of"]=> int(0) ["invert"]=> int(0) ["days"]=> bool(false) ["special_type"]=> int(0) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(0) ["have_special_relative"]=> int(0) }2 1Daniellehr tại gmx dot de ¶ ¶ kuzb ¶ object(DateInterval)#1 (16) { ["y"]=> int(0) ["m"]=> int(0) ["d"]=> int(9) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["f"]=> float(0) ["weekday"]=> int(0) ["weekday_behavior"]=> int(0) ["first_last_day_of"]=> int(0) ["invert"]=> int(0) ["days"]=> bool(false) ["special_type"]=> int(0) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(0) ["have_special_relative"]=> int(0) }4 object(DateInterval)#1 (16) { ["y"]=> int(0) ["m"]=> int(0) ["d"]=> int(9) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["f"]=> float(0) ["weekday"]=> int(0) ["weekday_behavior"]=> int(0) ["first_last_day_of"]=> int(0) ["invert"]=> int(0) ["days"]=> bool(false) ["special_type"]=> int(0) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(0) ["have_special_relative"]=> int(0) }5 object(DateInterval)#1 (16) { ["y"]=> int(0) ["m"]=> int(0) ["d"]=> int(9) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["f"]=> float(0) ["weekday"]=> int(0) ["weekday_behavior"]=> int(0) ["first_last_day_of"]=> int(0) ["invert"]=> int(0) ["days"]=> bool(false) ["special_type"]=> int(0) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(0) ["have_special_relative"]=> int(0) }6 object(DateInterval)#1 (16) { ["y"]=> int(0) ["m"]=> int(0) ["d"]=> int(9) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["f"]=> float(0) ["weekday"]=> int(0) ["weekday_behavior"]=> int(0) ["first_last_day_of"]=> int(0) ["invert"]=> int(0) ["days"]=> bool(false) ["special_type"]=> int(0) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(0) ["have_special_relative"]=> int(0) }7 object(DateInterval)#1 (16) { ["y"]=> int(0) ["m"]=> int(0) ["d"]=> int(9) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["f"]=> float(0) ["weekday"]=> int(0) ["weekday_behavior"]=> int(0) ["first_last_day_of"]=> int(0) ["invert"]=> int(0) ["days"]=> bool(false) ["special_type"]=> int(0) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(0) ["have_special_relative"]=> int(0) }8 jawzx01 tại gmail dot com ¶ ¶ Quản trị viên tại Torntech dot com 7 năm trước object(DateInterval)#1 (16) { ["y"]=> int(0) ["m"]=> int(0) ["d"]=> int(2) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["f"]=> float(0) ["weekday"]=> int(0) ["weekday_behavior"]=> int(0) ["first_last_day_of"]=> int(0) ["invert"]=> int(0) ["days"]=> bool(false) ["special_type"]=> int(0) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(0) ["have_special_relative"]=> int(0) }0 object(DateInterval)#1 (16) { ["y"]=> int(0) ["m"]=> int(0) ["d"]=> int(2) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["f"]=> float(0) ["weekday"]=> int(0) ["weekday_behavior"]=> int(0) ["first_last_day_of"]=> int(0) ["invert"]=> int(0) ["days"]=> bool(false) ["special_type"]=> int(0) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(0) ["have_special_relative"]=> int(0) }1 object(DateInterval)#1 (16) { ["y"]=> int(0) ["m"]=> int(0) ["d"]=> int(2) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["f"]=> float(0) ["weekday"]=> int(0) ["weekday_behavior"]=> int(0) ["first_last_day_of"]=> int(0) ["invert"]=> int(0) ["days"]=> bool(false) ["special_type"]=> int(0) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(0) ["have_special_relative"]=> int(0) }2 4 năm trước object(DateInterval)#1 (16) { ["y"]=> int(0) ["m"]=> int(0) ["d"]=> int(2) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["f"]=> float(0) ["weekday"]=> int(0) ["weekday_behavior"]=> int(0) ["first_last_day_of"]=> int(0) ["invert"]=> int(0) ["days"]=> bool(false) ["special_type"]=> int(0) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(0) ["have_special_relative"]=> int(0) }3 object(DateInterval)#1 (16) { ["y"]=> int(0) ["m"]=> int(0) ["d"]=> int(2) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["f"]=> float(0) ["weekday"]=> int(0) ["weekday_behavior"]=> int(0) ["first_last_day_of"]=> int(0) ["invert"]=> int(0) ["days"]=> bool(false) ["special_type"]=> int(0) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(0) ["have_special_relative"]=> int(0) }4 object(DateInterval)#1 (16) { ["y"]=> int(0) ["m"]=> int(0) ["d"]=> int(2) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["f"]=> float(0) ["weekday"]=> int(0) ["weekday_behavior"]=> int(0) ["first_last_day_of"]=> int(0) ["invert"]=> int(0) ["days"]=> bool(false) ["special_type"]=> int(0) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(0) ["have_special_relative"]=> int(0) }5 1Kevinpeno tại Gmail Dot Com ¶ ¶
object(DateInterval)#1 (16) { ["y"]=> int(0) ["m"]=> int(0) ["d"]=> int(2) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["f"]=> float(0) ["weekday"]=> int(0) ["weekday_behavior"]=> int(0) ["first_last_day_of"]=> int(0) ["invert"]=> int(0) ["days"]=> bool(false) ["special_type"]=> int(0) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(0) ["have_special_relative"]=> int(0) }7 object(DateInterval)#1 (16) { ["y"]=> int(0) ["m"]=> int(0) ["d"]=> int(2) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["f"]=> float(0) ["weekday"]=> int(0) ["weekday_behavior"]=> int(0) ["first_last_day_of"]=> int(0) ["invert"]=> int(0) ["days"]=> bool(false) ["special_type"]=> int(0) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(0) ["have_special_relative"]=> int(0) }8 1 |