When you compare the two integers keep in mind that the earlier the date/time is, the smaller its integer value will be. This is not important to you, though, because the two dates returned by strtotime() will have the same date and comparing the two variables will have the desired effect of comparing the two times as the dates cancel each other out. If your string doesn't have a date, only a time, strtotime() return today's date along with the time you gave in the string. Side note: strtotime() always returns a full date in integer form. strtotime() takes a string that is formatted as a date and converts it to the number of seconds since epoch. Of course since you'll most likely be receiving date/time values from page requests and database select queries you'll need to convert your date/time string into an integer before you can do any comparison or arithmetic.Īssuming you are sure that the $resttimefrom and $resttimeto variables contain properly formatted time you can use the strtotime() function to convert your string time into an integer. Once your date/time is in integer form you'll be able to efficiently compare it to other dates that are also in integer form. The integer repesentation of a date/time is the number of seconds since midnight, 1970-Jan-1, which is referred to as the 'epoch'. $difference = $datetime1->diff($datetime2) Įcho 'Difference: '.$difference->y.' years, 'ĭifference: 1 years, 1 months, 2 days Php.The trick to manipulating and comparing dates and times in PHP is to store date/time values in an integer variable and to use the mktime(), date() and strtotime() functions. You can use PHP new DateTime() function to get days difference between two dates. Output : 8 months Option 2: Finding the Number of Days Between two dates The following code help to find the Month’s difference between two dates. PHP Difference in Months Between two Dates
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |