Home » Php » Advance Php Interview Question » Calculate the difference between two dates in PHP.

Calculate the difference between two dates in PHP.

Calculate the difference between two dates in PHP

Calculate the diffrence between to date we do not substract directly like as a numeric number and substract two string.Genrally a date has six parameter years,  Months, Days, Hours, Minutes, and Seconds.If we have compare all these parameter it's take more time because first we explode these parametr and then compare.In php we have some in bulit function to compare and calcute the diffrence between two date.

date_diff(); date_diff is a php in bulit function that Return a new DateTime object, and then format the date.

date_format() : date_format function is used to display the date-time data in diffrent formate.first parameter is date which is formated and second one is formate type.

Another example to calculate the difference between two date in years, Months and Date

strtotime() : The function expects to be given a string containing a US English date format and parses an English textual datetime into a Unix timestamp (return the number of seconds since January 1 1970 00:00:00 GMT)

floor(): floor function returns the lowest integer value by rounding down value if required.

Format    Description
%a        Abbreviated weekday name
%b        Abbreviated month name
%c        Month, numeric
%D        Day of month with English suffix
%d        Day of month, numeric (00-31)
%e        Day of month, numeric (0-31)
%f        Microseconds
%H        Hour (00-23)
%h        Hour (01-12)
%I        Hour (01-12)
%i        Minutes, numeric (00-59)
%j        Day of year (001-366)
%k        Hour (0-23)
%l        Hour (1-12)
%M        Month name
%m        Month, numeric (00-12)
%p        AM or PM
%r        Time, 12-hour (hh:mm:ss AM or PM)
%S        Seconds (00-59)
%s        Seconds (00-59)
%T        Time, 24-hour (hh:mm:ss)
%U        Week (00-53) where Sunday is the first day of week
%u        Week (00-53) where Monday is the first day of week
%V        Week (01-53) where Sunday is the first day of week, used with %X
%v        Week (01-53) where Monday is the first day of week, used with %x
%W        Weekday name
%w        Day of the week (0=Sunday, 6=Saturday)
%X        Year for the week where Sunday is the first day of week, four digits, used with %V
%x        Year for the week where Monday is the first day of week, four digits, used with %v
%Y        Year, four digits
%y        Year, two digits

, , , , , ,

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">