Monty Shokeen Jul 10, 2021 Comparing dates becomes necessary when you are creating booking and event scheduling plugins or websites in PHP, It has many other uses as well, depending on how you implement things" /> Monty Shokeen Jul 10, 2021 Comparing dates becomes necessary when you are creating booking and event scheduling plugins or websites in PHP, It has many other uses as well, depending on how you implement things" />

logo

  • Hỏi Đáp
  • Kiến Thức
  • Sức Khỏe
  • Tử Vi
  • Công Nghệ
No Result
View All Result
logo
No Result
View All Result
Trang chủ how to compare two dates in php

How To Compare Two Dates In Php

by Admin _ June 11, 2022
post-promotion-placement#checkSidebarAdDisplayCondition" data-controller="scroll-position sticky-ad post-promotion-placement ">
*
Monty Shokeen Jul 10, 2021

Comparing dates becomes necessary when you are creating booking and sự kiện scheduling plugins or websites in PHP. It has many other uses as well, depending on how you implement things. For example, we might use it to lớn create time-sensitive promotional events in our PHP scripts.

Bạn đang xem: How to compare two dates in php

PHP comes with dedicated functions và methods that you can use khổng lồ compare dates when writing procedural or object-oriented style darkedeneurope.com. In this tutorial, we will discuss them both.

Compare Dates in PHP (Object-Oriented Style)

There is a DateTime class in PHP that you can use to lớn create DateTime objects. You can pass it a valid date/time string along with an optional timezone argument to lớn create a DateTime object. Here are a few examples:

You can use simple comparison operators on these DateTime objects lớn compare the dates now and check whether the returned value is true or false.

$dist_past) echo "The third date is in the distant past.";// Output: The third date is in the distant past.if($dist_future > $now) echo "The fourth date is in the future.";// Output: The fourth date is in the future.?>

Difference of Dates in PHP (Object-Oriented Style)

Sometimes, you might want lớn get some more information when comparing dates. There are dedicated methods and functions in PHP to calculate the difference between dates.

Once you have created your DateTime objects, you can also call the diff() method on one object và pass it the other date in order to lớn calculate the difference between the dates. This will give you back a DateInterval object.

Keep in mind that the date on which you called diff() will be subtracted from the date passed lớn this method. You can format the difference any way you lượt thích after that by using the format() method. Here are some examples:

diff($now);$days_next = $now->diff($next);echo $days_last->format("%a days")." since last Christmas.";// Output: 170 days since last Christmas.echo $days_next->format("%a days")." lớn next Christmas.";// Output: 194 days khổng lồ next Christmas.?>You can pass many parameters khổng lồ the format() method lớn control how PHP outputs the difference. In our example, we used %r to lớn determine the relativity of the difference. A positive value means that the date passed lớn diff() is in the future in comparison to lớn the date which called it. A negative value means that the date passed to diff() is in the past in comparison to the date which called it. The %a string is used to show the number of days and hours. You can also use other strings lượt thích %H, %I, và %S lớn show hours, minutes, và seconds respectively. All these format characters can be found in the documentation for the format() method.

Xem thêm: " Ldk Là Gì - Ldk Là Gì, Nghĩa Của Từ Ldk

Compare Dates in PHP (Procedural Style)

You can use a different set of functions if you prefer to write procedural style darkedeneurope.com. In this case, we can use the date_create() function to lớn create our DateTime objects from the passed string. The variables you define will still store DateTime objects, but they won"t be explicitly instantiated by you.

Since we are still dealing with DateTime objects, the regular comparison operators will still allow us to compare these dates.

$dist_past) echo "The third date is in the distant past.";// Output: The third date is in the distant past.if($dist_future > $now) echo "The fourth date is in the future.";// Output: The fourth date is in the future.?>

Difference of Dates in PHP (Procedural Style)

Just lượt thích the diff() method of the DateTime class, there is a corresponding date_create() function that you can use khổng lồ calculate the difference between two dates. It accepts two parameters, & the date provided in the first parameter is subtracted from the date in the second parameter.

Here"s an example to lớn calculate the number of days since last Christmas & the number of days to lớn next Christmas.

We wanted lớn keep the returned number of days positive in both cases, so the time that comes later is passed as the second parameter.

Final Thoughts

PHP comes with handy methods và functions lớn compare two dates, depending on your coding style. You can get more information like the exact time difference between two dates using the diff() method và the date_diff() function in PHP. I have tried khổng lồ cover the basics of comparing dates in PHP in this tutorial. You should consider reading the documentation lớn learn how to format the difference in dates in more advanced ways.

Share Tweet Linkedin Pinterest
Previous Post

Học lập trình php căn bản

Next Post

How to set multiple cookies by using php header

CÙNG CHUYÊN MỤC

form register php

Form register php

09/04/2021
fantastic blog (cms) in php with source code

Fantastic blog (cms) in php with source code

28/04/2021
validate form php javascript

Validate form php javascript

28/04/2021
http diendanlequydon com viewtopic php style 6

Http diendanlequydon com viewtopic php style 6

28/04/2021
cách tính năm nhuận trong php

Cách tính năm nhuận trong php

04/07/2022
bản khai đăng ký tên miền

Bản khai đăng ký tên miền

02/07/2022
phần mềm lập trình php tốt nhất

Phần mềm lập trình php tốt nhất

01/07/2022
ghi file trong php

Ghi file trong php

30/06/2022

Newsletter

The most important automotive news and events of the day

We won't spam you. Pinky swear.

Chuyên Mục

  • Hỏi Đáp
  • Kiến Thức
  • Sức Khỏe
  • Tử Vi
  • Công Nghệ

News Post

  • Impressum facebook là gì

About

Chúng tôi tạo ra trang web nhằm mục đích mang lại kiến thức bổ ích cho cộng đồng, các bài viết được sưu tầm từ nhiều nguồn trên internet giúp mang lại kiến thức khách quan dành cho bạn

©2022 darkedeneurope.com - Website WordPress vì mục đích cộng đồng

Liên Hệ - Giới Thiệu - Nội Quy - Bảo Mật

No Result
View All Result
  • Trang chủ
  • Chuyên mục
    • Hỏi Đáp
    • Kiến Thức
    • Sức Khỏe
    • Tử Vi
    • Công Nghệ
  • Lưu trữ
  • Liên hệ

© 2022 darkedeneurope.com - Website WordPress vì mục đích cộng đồng.