Bạn đang xem: Compare two datetime php code example
Continue reading to compare dates in multiple ways và find the difference between the given dates while leaving zero excuses behind.
ContentsHow khổng lồ Compare Timestamps Given as Text?How to lớn Compare Two Dates Given in Different Formats?How khổng lồ Perform PHP Date Comparison With Present Date?How khổng lồ Compare Two Dates & Find Difference?How to lớn Compare Date With Month and Year Only?
How khổng lồ 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 ”=”, và less than or equal to lớn “PHP Date Comparison Coding Examples
For example, you have the joining dates of two employees stored as strings. Now, you want khổng lồ 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 lớn PHP compare date efficiently:
You will notice the given đầu ra on your screen:
|Employee One Joined Last.|
How lớn Compare Timestamps Given as Text?
You can use the strtotime() function to lớn 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” & “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 & another student enrolled two days after Christmas. Now, you want to lớn see which student enrolled first. Therefore, you’ll use the strtotime() function khổng lồ get the timestamps, và 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 lớn get timestamps$timestamp1 = strtotime($std1);$timestamp2 = strtotime($std2);// comparing the timestampsif($timestamp1 > $timestamp2)echo “Student Two Enrolled on First.”;elseecho “Student One Enrolled First.”;?>|
You will see the below result in your browser window:
|Student One Enrolled First.|
How to lớn Compare Two Dates Given in Different Formats?
You can use the DateTime Class objects to khuyến mãi 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.
– Coding Script
For instance, you have dates in different formats & 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 & how you can compare them:
How lớn Perform PHP Date Comparison With Present Date?
The strtotime() function along with the comparison operators also helps to lớn PHP date compare with the current date. You can get the current date by using strtotime(“today”) & specify another date lượt thích 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 khung submission. Now, you want to compare the given date with the current date and display an appropriate message lượt thích 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 & 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 và 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 lớn track the dates. Here, you have all the dates but you want to get the number of days or months between your first order & 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 lớn see the number of days or months that have been passed between your first và ninth order.
Please look below to gain access to lớn the code block that implements the above-stated functionality to lớn 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 lớn Compare Date With Month and Year Only?
You can use the comparison operators to lớn compare a date with only a month và a year. Surely, you’ll get satisfying results without any errors.
– Coding Example
Here is a coding block that compares the date with only a month and a year:
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 khổng lồ PHP compare two dates having the same formatsYou can use the strttotime() function to lớn 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 lớn compare two dates và find the difference between them