*

The PHP date compare task can be achieved by using the comparison operators along with the strtotime() function. Also, you can convert different date strings into DateTime objects before comparing the same. So, this post talks about comparing the dates specified in the same format along with the ones written in different formats including the textual date strings.

Đang xem: Compare two datetime php code example

Continue reading to compare dates in multiple ways and find the difference between the given dates while leaving zero excuses behind.

Contents

How To Compare Timestamps Given as Text?How To Compare Two Dates Given in Different Formats?How To Perform PHP Date Comparison With Present Date?How To Compare Two Dates and Find Difference?How To Compare Date With Month and Year Only?

How To Compare Date in PHP?

You can PHP compare two dates given in the same formats by using the comparison operators. These operators include the greater than “>”, less than ”=”, and less than or equal to “PHP Date Comparison Coding Examples

For example, you have the joining dates of two employees stored as strings. Now, you want to see which employee joined your company first. So, you’ll simply PHP compare two dates by using the greater than “>” operator. Consequently, you’ll print the statements as per the obtained result.

Please look at the example code block to PHP compare date efficiently:

You will notice the given output on your screen:

Employee One Joined Last.

How To Compare Timestamps Given as Text?

You can use the strtotime() function to convert the date strings to Unix timestamps. Next, you can PHP compare timestamps by using the comparison operators. So, if you have dates written as “last Sunday” and “First Friday of January 2022”, you can compare them easily by using the strtotime() function.

– Coding Example

For example, a student got admitted to the school in the last week of December and another student enrolled two days after Christmas. Now, you want to see which student enrolled first. Therefore, you’ll use the strtotime() function to get the timestamps, and then PHP compare timestamps with the help of comparison operators.

Here is the fragment of code that shows what the above example is trying to explain:

// defining textual date strings$std1 = “Last Week of December”;$std2 = “Two days after the Christmas”;// using the strtotime() function to get timestamps$timestamp1 = strtotime($std1);$timestamp2 = strtotime($std2);// comparing the timestampsif($timestamp1 > $timestamp2){echo “Student Two Enrolled on First.”;}else{echo “Student One Enrolled First.”;}?>

You will see the below result in your browser window:

Student One Enrolled First.

How To Compare Two Dates Given in Different Formats?

You can use the DateTime Class objects to deal with the dates provided in different formats. It doesn’t matter if you have written the year in four digits or two digits. Therefore, you will get satisfying results irrespective of the way you’ve added the year.

Moreover, the DateTime considers the first specified two digits as “year”. However, if you specify a four-digit year in the end then the given object understands it well.

Xem thêm: Liên Quân Bảo Trì Ngày Hôm Nay Đến Mấy Giờ? Liên Quân Bảo Trì Hôm Nay Đến Mấy Giờ

– Coding Script

For instance, you have dates in different formats and now, you want to PHP date compare. So, here you’ll create the DateTime objects for each of the given dates and compare the same later.

The below coding script contains dates in different formats showing you how the DateTime class parses the given dates and how you can compare them:

How To Perform PHP Date Comparison With Present Date?

The strtotime() function along with the comparison operators also helps to PHP date compare with the current date. You can get the current date by using strtotime(“today”) and specify another date like strtotime(“your date”). Next, you’ll perform a PHP date comparison by using the comparison operators.

– Code Block

Let’s assume you have created a program that contains the last date of form submission. Now, you want to compare the given date with the current date and display an appropriate message like the code snippet shared here:

How To Compare Two Dates and Find Difference?

The date_diff() function lets you compare dates and find their difference. The given function accepts two dates that are either created by using the date_create() function or the DateTime class. Next, the function subtracts the first date from the second date and returns the difference between the dates.

You can see the syntax here: date_diff(date1, date2).

Once you get the difference between the dates, you can then format the same and print it on your screen by typing a statement similar to “echo “$result->format(“%formatSpecifier”).”

– Example Code

For instance, you have developed a management system to track the dates. Here, you have all the dates but you want to get the number of days or months between your first order and ninth order. So, you’ll take the dates from the record, create DateTime objects for the same, and execute the date_diff() function. Eventually, you can choose to see the number of days or months that have been passed between your first and ninth order.

Please look below to gain access to the code block that implements the above-stated functionality to PHP compare two dates and find difference:

Here is the output that you’ll see on your screen:

+8 Months or +248 days.

Note:you can specify any date format specifiers to see the difference between the dates.

How To Compare Date With Month and Year Only?

You can use the comparison operators to compare a date with only a month and a year. Surely, you’ll get satisfying results without any errors.

Xem thêm: Tải Game Kuroko No Basuke – Kuroko No Basuke Game Apk No 1 Best Apk

– Coding Example

Here is a coding block that compares the date with only a month and a year:

Conclusion

It is obvious from the above discussion that you can PHP date compare with the help of the comparison operators. Moreover, the issues with the date formats can be resolved by using the strtotime() function or the DateTime objects before the comparison. Please go through the points stated below to PHP compare the two dates without spending much time:

The comparison operators can be used to PHP compare two dates having the same formatsYou can use the strttotime() function to convert textual dates into timestamps before comparing the sameYou can PHP compare two dates having different formats by using the DateTime objectsYou can PHP date compare with current date by using the strtotime() function along with comparison operatorsThe date_diff() function allows you to compare two dates and find the difference between them

*

So, now you don’t need to think if the dates are written in YY-MM-DD, YYYY-MM-DD, or text format, you can still compare the dates and get your work done.

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *