![]() PHP Tutorial PHP - Introduction PHP - Installation PHP - Syntax PHP - Variables PHP - Echo PHP - Strings PHP - Operators PHP - Comments PHP - Include File PHP - Require PHP - If Statement PHP - If. Miscellaneous XML Tutorial Webhost Tutorial Flash Tutorial SEO Tutorial Scripting Javascript Tutorial PHP Tutorial Perl Tutorial Ajax Tutorial ASP Tutorial VBScript Tutorial DateInterval::invert will be 1 if the second date is before the first date, and 0 if the the second date is on or after the first date.Web Tutorials Beginner Tutorial HTML Tutorial CSS Tutorial If a DateInterval object was returned, you can check the 'invert' property to see if the second date is before the first date or not. Var_dump ( $now -> diff ( $then )) // DateInterval object It's better to use DateTime::diff() because the return value will only be a DateInterval object if the types were compatible, otherwise it will be false. I just found a subtle bug in my code because it was comparing a date against an uninitialised variable. If someone also found this behaviour I'd like to hear about it - give me a quick mail at schindhelm (at) gmail (dot) com.Īlthough you CAN directly compare DateTime objects, you will get nonintuitive results if the other object is not also DateTime compatible. Note that 3 days may be a lot if you want to create invoices and have to check against a given age to determine if the customer is chargable for taxes and so on. When calculating with the date() function it was more accurate (didn't use seconds/hours for comparison). ![]() ![]() $birth = new DateTime ( '' ) // three days difference!Įcho $diff -> format ( '%y' ) // will output 44 - correct Here's what I got:Įcho $diff -> format ( '%y' ) // will output 45Įcho $diff -> format ( '%y' ) // will output 45 wrongly I calculated the age gap between now and a birthdate from before 1970 (unix epoch). ![]() I found that DateTime::diff isn't as accurate as I thought. Therefore I hope this can help some others: Though I found a number of people who ran into the issue of 5.2 and lower not supporting this function, I was unable to find any solid examples to get around it. Return pluralize ( $interval -> s, 'second' ). If ( $v = $interval -> i >= 1 ) return pluralize ( $interval -> i, 'minute' ). If ( $v = $interval -> h >= 1 ) return pluralize ( $interval -> h, 'hour' ). If ( $v = $interval -> d >= 1 ) return pluralize ( $interval -> d, 'day' ). If ( $v = $interval -> m >= 1 ) return pluralize ( $interval -> m, 'month' ). If ( $v = $interval -> y >= 1 ) return pluralize ( $interval -> y, 'year' ). $suffix = ( $interval -> invert ? ' ago' : '' ) If the date occurs in the past (is negative/inverted), it suffixes it with 'ago'. The function below returns a single number of years, months, days, hours, minutes or seconds between the current date and the provided date. If you want to quickly scan through the resulting intervals, you can use the undocumented properties of DateInterval. ![]() Getting Started Introduction A simple tutorial Language Reference Basic syntax Types Variables Constants Expressions Operators Control Structures Functions Classes and Objects Namespaces Enumerations Errors Exceptions Fibers Generators Attributes References Explained Predefined Variables Predefined Exceptions Predefined Interfaces and Classes Context options and parameters Supported Protocols and Wrappers Security Introduction General considerations Installed as CGI binary Installed as an Apache module Session Security Filesystem Security Database Security Error Reporting User Submitted Data Hiding PHP Keeping Current Features HTTP authentication with PHP Cookies Sessions Dealing with XForms Handling file uploads Using remote files Connection handling Persistent Database Connections Command line usage Garbage Collection DTrace Dynamic Tracing Function Reference Affecting PHP's Behaviour Audio Formats Manipulation Authentication Services Command Line Specific Extensions Compression and Archive Extensions Cryptography Extensions Database Extensions Date and Time Related Extensions File System Related Extensions Human Language and Character Encoding Support Image Processing and Generation Mail Related Extensions Mathematical Extensions Non-Text MIME Output Process Control Extensions Other Basic Extensions Other Services Search Engine Extensions Server Specific Extensions Session Extensions Text Processing Variable and Type Related Extensions Web Services Windows Only Extensions XML Manipulation GUI Extensions Keyboard Shortcuts ? This help j Next menu item k Previous menu item g p Previous man page g n Next man page G Scroll to bottom g g Scroll to top g h Goto homepage g s Goto search ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |