Hướng dẫn php convert dateinterval to int - php chuyển đổi dateinterval thành int

8

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Tôi đang sử dụng mã này:

$due_date = new DateTime[$_POST['due_date']];

$today = new DateTime[];
$months = $due_date->diff[$today];

$months->format["%m"];

$fine = 0.02 * $price * $months; // i got error in this line

$bill = $price + $fine;

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à:

Object of class DateInterval could not be converted to int

Đã 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ì $months không phải là INT, mà là một đối tượng DateTime như thế này:

DateInterval Object
[
    [y] => 0
    [m] => 4
    [d] => 12
    [h] => 6
    [i] => 56
    [s] => 9
    [weekday] => 0
    [weekday_behavior] => 0
    [first_last_day_of] => 0
    [invert] => 0
    [days] => 133
    [special_type] => 0
    [special_amount] => 0
    [have_weekday_relative] => 0
    [have_special_relative] => 0
]

Bạn có thể nhận được giá trị số nguyên của những tháng như thế này

$due_date = new DateTime['13-02-2016'];

$today = new DateTime[];
$months = $due_date->diff[$today];

echo $months->m;

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ư

$due_date = new DateTime[$_POST['due_date']];

$today = new DateTime[];
$months = $due_date->diff[$today];

$fine = 0.02 * $price * $months->m; // i got no error in this line

$bill = $price + $fine;

Đã 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 format đư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:

$due_date = new DateTime[$_POST['due_date']];

$today = new DateTime[];
$diff = $due_date->diff[$today];

$months = $diff->format["%m"];

$fine = 0.02 * $price * $months;

$bill = $price + $fine;

Đã 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:

$due_date = new DateTime['13-02-2016'];

$today = new DateTime[];
$diff = $due_date->diff[$today];

$months = [$diff->y * 12] + $diff->m;

echo $months;

Kiểm tra nó trong hộp cát: //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

Chức năng nào sẽ tạo đối tượng DateInterVal từ các phần tương đối của chuỗi?

Hàm DATE_Interval_Create_From_Date_String [] đặt ra một ngày Xác định từ các phần tương đối của chuỗi.Formats the interval

Sự mô tả

Định dạng

$due_date = new DateTime['13-02-2016'];

$today = new DateTime[];
$diff = $due_date->diff[$today];

$months = [$diff->y * 12] + $diff->m;

echo $months;
9 và
31 total days
1 month, 0 days
3 bây giờ sẽ luôn luôn tích cực. DateInterval::format[string
Object of class DateInterval could not be converted to int
0
]: string

Các ký tự định dạng
$due_date = new DateTime['13-02-2016'];

$today = new DateTime[];
$diff = $due_date->diff[$today];

$months = [$diff->y * 12] + $diff->m;

echo $months;
9 và
31 total days
1 month, 0 days
3 đã được thêm vào.

Ví dụ The following characters are recognized in the format parameter string. Each format character must be prefixed by a percent sign [
Object of class DateInterval could not be converted to int
3].
Ví dụ #1 ví dụSự mô tảĐịnh dạng
$due_date = new DateTime['13-02-2016'];

$today = new DateTime[];
$diff = $due_date->diff[$today];

$months = [$diff->y * 12] + $diff->m;

echo $months;
9 và
31 total days
1 month, 0 days
3 bây giờ sẽ luôn luôn tích cực.
Các ký tự định dạng
$due_date = new DateTime['13-02-2016'];

$today = new DateTime[];
$diff = $due_date->diff[$today];

$months = [$diff->y * 12] + $diff->m;

echo $months;
9 và
31 total days
1 month, 0 days
3 đã được thêm vào.
Ví dụCác ký tự định dạng
$due_date = new DateTime['13-02-2016'];

$today = new DateTime[];
$diff = $due_date->diff[$today];

$months = [$diff->y * 12] + $diff->m;

echo $months;
9 và
31 total days
1 month, 0 days
3 đã được thêm vào.
Ví dụVí dụ #1 ví dụVí dụ trên sẽ xuất ra:
Ví dụ #2 DateInterval và mang các điểmVí dụ #3 DateInterVal và DateTime :: Diff [] với %A và %D sửa đổiGhi chú
Ghi chú: Phương thức DateInterval :: Định dạng [] không tính toán lại các điểm mang theo trong các chuỗi thời gian cũng như trong các phân đoạn ngày. Điều này được mong đợi bởi vì không thể tràn các giá trị như format9 có thể được hiểu là bất cứ điều gì từ
Object of class DateInterval could not be converted to int
00 đến
Object of class DateInterval could not be converted to int
01.
Không có người dùng đóng góp ghi chú cho trang này.
DateInterval trong PHP là gì?Hàm DateInterVal :: Định dạng [] được sử dụng để định dạng khoảng thời gian.Làm cách nào để sử dụng DateInterVal?
Một cách phổ biến để tạo đối tượng DateInterVal là tính toán chênh lệch giữa hai đối tượng ngày/thời gian thông qua DateTimeInterface :: Diff []. Vì không có cách nào được xác định rõ để so sánh các khoảng thời gian ngày, nên các phiên bản ngày không thể so sánh được.DateInterval là gì?Khoảng thời gian giữa ngày bắt đầu cụ thể và ngày kết thúc.
Chức năng nào sẽ tạo đối tượng DateInterVal từ các phần tương đối của chuỗi?Hàm DATE_Interval_Create_From_Date_String [] đặt ra một ngày Xác định từ các phần tương đối của chuỗi.[Php 5> = 5.3.0, Php 7, Php 8]
DateInterval :: định dạng - định dạng khoảng thời gianPublicDateInterval :: Định dạng [Chuỗi ____10]: ChuỗiDateTime::diff[] or
$due_date = new DateTime[$_POST['due_date']];

$today = new DateTime[];
$months = $due_date->diff[$today];

$fine = 0.02 * $price * $months->m; // i got no error in this line

$bill = $price + $fine;
1 otherwise
Thông số
format Các ký tự sau được nhận ra trong chuỗi tham số format. Mỗi ký tự định dạng phải được tiền tố bằng một dấu hiệu phần trăm [
Object of class DateInterval could not be converted to int
3].
format nhân vậtGiá trị ví dụ
Object of class DateInterval could not be converted to int
3
Giờ, số
DateInterval Object
[
    [y] => 0
    [m] => 4
    [d] => 12
    [h] => 6
    [i] => 56
    [s] => 9
    [weekday] => 0
    [weekday_behavior] => 0
    [first_last_day_of] => 0
    [invert] => 0
    [days] => 133
    [special_type] => 0
    [special_amount] => 0
    [have_weekday_relative] => 0
    [have_special_relative] => 0
]
2,
DateInterval Object
[
    [y] => 0
    [m] => 4
    [d] => 12
    [h] => 6
    [i] => 56
    [s] => 9
    [weekday] => 0
    [weekday_behavior] => 0
    [first_last_day_of] => 0
    [invert] => 0
    [days] => 133
    [special_type] => 0
    [special_amount] => 0
    [have_weekday_relative] => 0
    [have_special_relative] => 0
]
3,
$due_date = new DateTime[$_POST['due_date']];

$today = new DateTime[];
$months = $due_date->diff[$today];

$fine = 0.02 * $price * $months->m; // i got no error in this line

$bill = $price + $fine;
8
$due_date = new DateTime[$_POST['due_date']];

$today = new DateTime[];
$diff = $due_date->diff[$today];

$months = $diff->format["%m"];

$fine = 0.02 * $price * $months;

$bill = $price + $fine;
3
Phút, số, ít nhất 2 chữ số với hàng đầu 0
Object of class DateInterval could not be converted to int
9,
DateInterval Object
[
    [y] => 0
    [m] => 4
    [d] => 12
    [h] => 6
    [i] => 56
    [s] => 9
    [weekday] => 0
    [weekday_behavior] => 0
    [first_last_day_of] => 0
    [invert] => 0
    [days] => 133
    [special_type] => 0
    [special_amount] => 0
    [have_weekday_relative] => 0
    [have_special_relative] => 0
]
0,
$due_date = new DateTime[$_POST['due_date']];

$today = new DateTime[];
$diff = $due_date->diff[$today];

$months = $diff->format["%m"];

$fine = 0.02 * $price * $months;

$bill = $price + $fine;
6
$due_date = new DateTime[$_POST['due_date']];

$today = new DateTime[];
$diff = $due_date->diff[$today];

$months = $diff->format["%m"];

$fine = 0.02 * $price * $months;

$bill = $price + $fine;
7
Phút, số
DateInterval Object
[
    [y] => 0
    [m] => 4
    [d] => 12
    [h] => 6
    [i] => 56
    [s] => 9
    [weekday] => 0
    [weekday_behavior] => 0
    [first_last_day_of] => 0
    [invert] => 0
    [days] => 133
    [special_type] => 0
    [special_amount] => 0
    [have_weekday_relative] => 0
    [have_special_relative] => 0
]
2,
DateInterval Object
[
    [y] => 0
    [m] => 4
    [d] => 12
    [h] => 6
    [i] => 56
    [s] => 9
    [weekday] => 0
    [weekday_behavior] => 0
    [first_last_day_of] => 0
    [invert] => 0
    [days] => 133
    [special_type] => 0
    [special_amount] => 0
    [have_weekday_relative] => 0
    [have_special_relative] => 0
]
3,
$due_date = new DateTime[$_POST['due_date']];

$today = new DateTime[];
$diff = $due_date->diff[$today];

$months = $diff->format["%m"];

$fine = 0.02 * $price * $months;

$bill = $price + $fine;
6
$due_date = new DateTime['13-02-2016'];

$today = new DateTime[];
$diff = $due_date->diff[$today];

$months = [$diff->y * 12] + $diff->m;

echo $months;
1
Giây, số, ít nhất 2 chữ số với số 0
Object of class DateInterval could not be converted to int
9,
DateInterval Object
[
    [y] => 0
    [m] => 4
    [d] => 12
    [h] => 6
    [i] => 56
    [s] => 9
    [weekday] => 0
    [weekday_behavior] => 0
    [first_last_day_of] => 0
    [invert] => 0
    [days] => 133
    [special_type] => 0
    [special_amount] => 0
    [have_weekday_relative] => 0
    [have_special_relative] => 0
]
0,
$due_date = new DateTime['13-02-2016'];

$today = new DateTime[];
$diff = $due_date->diff[$today];

$months = [$diff->y * 12] + $diff->m;

echo $months;
4
$due_date = new DateTime['13-02-2016'];

$today = new DateTime[];
$diff = $due_date->diff[$today];

$months = [$diff->y * 12] + $diff->m;

echo $months;
5
Giây, số
DateInterval Object
[
    [y] => 0
    [m] => 4
    [d] => 12
    [h] => 6
    [i] => 56
    [s] => 9
    [weekday] => 0
    [weekday_behavior] => 0
    [first_last_day_of] => 0
    [invert] => 0
    [days] => 133
    [special_type] => 0
    [special_amount] => 0
    [have_weekday_relative] => 0
    [have_special_relative] => 0
]
2,
DateInterval Object
[
    [y] => 0
    [m] => 4
    [d] => 12
    [h] => 6
    [i] => 56
    [s] => 9
    [weekday] => 0
    [weekday_behavior] => 0
    [first_last_day_of] => 0
    [invert] => 0
    [days] => 133
    [special_type] => 0
    [special_amount] => 0
    [have_weekday_relative] => 0
    [have_special_relative] => 0
]
3,
$due_date = new DateTime['13-02-2016'];

$today = new DateTime[];
$diff = $due_date->diff[$today];

$months = [$diff->y * 12] + $diff->m;

echo $months;
4
$due_date = new DateTime['13-02-2016'];

$today = new DateTime[];
$diff = $due_date->diff[$today];

$months = [$diff->y * 12] + $diff->m;

echo $months;
9
Micro giây, số, ít nhất 6 chữ số với số 0
31 total days
1 month, 0 days
0,
31 total days
1 month, 0 days
1,
31 total days
1 month, 0 days
2
31 total days
1 month, 0 days
3
Micro giây, số
31 total days
1 month, 0 days
4,
31 total days
1 month, 0 days
5,
31 total days
1 month, 0 days
2
31 total days
1 month, 0 days
7
Ký "
31 total days
1 month, 0 days
8" khi âm, "
31 total days
1 month, 0 days
9" khi dương
31 total days
1 month, 0 days
8,
31 total days
1 month, 0 days
9
$months2Ký "
31 total days
1 month, 0 days
8" khi âm, trống khi dương
31 total days
1 month, 0 days
8, $months5

Trả về giá trị

Trả về khoảng định dạng.

Thay đổi

Phiên bảnSự mô tả
7.2.12 Định dạng
$due_date = new DateTime['13-02-2016'];

$today = new DateTime[];
$diff = $due_date->diff[$today];

$months = [$diff->y * 12] + $diff->m;

echo $months;
9 và
31 total days
1 month, 0 days
3 bây giờ sẽ luôn luôn tích cực.
7.1.0 Các ký tự định dạng
$due_date = new DateTime['13-02-2016'];

$today = new DateTime[];
$diff = $due_date->diff[$today];

$months = [$diff->y * 12] + $diff->m;

echo $months;
9 và
31 total days
1 month, 0 days
3 đã được thêm vào.

Ví dụ

Ví dụ #1 ví dụDateInterval example

format0

format1

format2

Ví dụ trên sẽ xuất ra:

Ví dụ #2 DateInterval và mang các điểmDateInterval and carry over points

format0

format1

format5

Ví dụ trên sẽ xuất ra:

Ví dụ #2 DateInterval và mang các điểmDateInterval and DateTime::diff[] with the %a and %d modifiers

format0

format7

format8

Ví dụ trên sẽ xuất ra:

31 total days
1 month, 0 days

Ghi chú

Ghi chú::

Phương thức DateInterval :: Định dạng [] không tính toán lại các điểm mang theo trong các chuỗi thời gian cũng như trong các phân đoạn ngày. Điều này được mong đợi bởi vì không thể tràn các giá trị như format9 có thể được hiểu là bất cứ điều gì từ

Object of class DateInterval could not be converted to int
00 đến
Object of class DateInterval could not be converted to int
01.DateInterval::format[] method does not recalculate carry over points in time strings nor in date segments. This is expected because it is not possible to overflow values like format9 which could be interpreted as anything from
Object of class DateInterval could not be converted to int
00 to
Object of class DateInterval could not be converted to int
01.

Không có người dùng đóng góp ghi chú cho trang này.

DateInterval trong PHP là gì?

Hàm DateInterVal :: Định dạng [] được sử dụng để định dạng khoảng thời gian.used to format the interval.

Làm cách nào để sử dụng DateInterVal?

Một cách phổ biến để tạo đối tượng DateInterVal là tính toán chênh lệch giữa hai đối tượng ngày/thời gian thông qua DateTimeInterface :: Diff [].Vì không có cách nào được xác định rõ để so sánh các khoảng thời gian ngày, nên các phiên bản ngày không thể so sánh được.calculating the difference between two date/time objects through DateTimeInterface::diff[]. Since there is no well defined way to compare date intervals, DateInterval instances are incomparable.

DateInterval là gì?

Khoảng thời gian giữa ngày bắt đầu cụ thể và ngày kết thúc..

Chức năng nào sẽ tạo đối tượng DateInterVal từ các phần tương đối của chuỗi?

Hàm DATE_Interval_Create_From_Date_String [] đặt ra một ngày Xác định từ các phần tương đối của chuỗi.date_interval_create_from_date_string[] function sets up a DateInterval from the relative parts of the string.

Bài Viết Liên Quan

Chủ Đề