How Do I Compare Two Datetime Objects In Php 5

a clockWhen I tìm kiếm and read code about time and date problems developers have, I noticed that a lot of people still use the old PHP functions lượt thích date(), time() or strtotime().

Bạn đang xem: How do i compare two datetime objects in php 5

What about using the class DateTime instead?

DateTime can bởi vì all the usual date và time operations you could ask for, và even more. Using DateTime can save a lot of time when you have to vì chưng simple, or more complex operations on dates.

Now I see you asking: why using DateTime instead of a bunch of PHP date functions?

Here’s why:

DateTime is definitely more robust to lớn use. Less bug is always better.DateTime is a class. You can use composition to lớn modify his behavior easily. You need always the same date as input đầu vào for your unit tests? Use your own instance of DateTime which always return the birthday of your dog!Using one class is easier khổng lồ understand for your fellow developer colleagues. Even if they don’t know DateTime, they just have to lớn look khổng lồ the documentation for one single class. It is definitely better than looking for a bunch of independent functions.

DateTime Instantiation và Formatting

The instantiation is easy: you can pass to the constructor a date correctly formatted, or nothing if you want to use the actual date and time.

Here the list of supported date and time formats you can inject in the constructor.

You can then format the date in order lớn display it, in two lines of code:



$dateTime = new DateTime();echo $dateTime->format('Y-m-d H:i:s');`
Output: whatever the current date is, correctly formatted.

Easy, isn’t it? You can as well precise the timezone you want as a second argument:


DateTime and Timestamps

You want to lớn format an unreadable timestamp into a nice và shiny easy-to-read date?


$dateTime = new DateTime();$dateTime->setTimestamp(1271802325);echo $dateTime->format('Y-m-d H:i:s');

Obviously you can as well đầu ra a timestamp if you need to:


Output: 1271802325

Adding or Subtracting Time

What about adding or retrieving a day, a minute, an hour of a date? You only need lớn use the proper formatting:


You can as well use the constructor if you work on the current date:


$dateTime = new DateTime('+1d');echo $dateTime->format('Y-m-d H:i:s');`
Output the current date plus one day.

For the same result, you could as well instantiate the class DateInterval but you would have to use an interval specification which is way harder to lớn read.

I never use this last solution personally, simply because I never need to.

Xem thêm: hahabar com c 1 html

Interval Between Different Dates

It’s very easy to lớn use DateTime classes to lớn compare dates:


Output: 00-0-1 22:0:0

In this example we created two DateTime objects. They will receive two different dates in their constructors.

In order to compare those two dates we use the method diff() of the first DateTime object with the second DateTime object as argument.

The diff() method will return a new object of type DateInterval. If you want khổng lồ format the difference between the two dates, you can use as well the format() method of the class DateInterval but be careful: it won’t accept the same formatting as the DateTime::format() method.

The output indicate that there is 1 day và 22 hours difference between the two dates.

If you only want the seconds, hours or day of interval you can write:


Output:

0221

Comparing DateTimes

If you need lớn compare two instances of DateTime, it’s as simple as writing:


Output: datetime1 lesser than datetime2

Keep in mind that you need PHP 5.2.2 or greater for the comparisons lớn work. I really hope it’s the case!

You’re Now a Master of DateTime!

I invite you khổng lồ read the PHP documentation about the class DateTime. You will never have to mess around with multiple functions & you will simplify your code a big time. Your colleague will praise you and your skills till the kết thúc of time.

Glory và fame will be at your door!

If you know other tricks and techniques with DateTime, feel không tính phí to giới thiệu your knowledge in the phản hồi section.

Let"s Connect

You"ll receive each month the last article with additional resources and updates.

Here"s how it looks

You can reply khổng lồ any e-mail if you have questions, problems, or feedback. I"ll write back as soon as I can.


Share Your Knowledge

PHP Code quality Tools to kiểm tra and Improve your Code

The DRY Principle: Benefits & Costs with Examples

The Valuable Dev All right reserved - 2021Proudly generated by HUGO