Skip to content
Advertisement

performing datetime related operations in PHP

How do you actually perform datetime operations such as adding date, finding difference, find out how many days excluding weekends in an interval? I personally started to pass some of these operations to my postgresql dbms as typically I would only need to issue one sql statement to obtain an answer, however, to do it in PHP way I would have to write a lot more code that means more chances for errors to occur…

Are there any libraries in PHP that does datetime operation in a way that don’t require a lot of code? that beats sql in a situation where ‘Given two dates, how many workdays are there between the two dates? Implement in either SQL, or $pet_lang’ that is solved by making this query?

Advertisement

Answer

PHP5+’s DateTime object is useful because it is leap time and daylight savings aware, but it needs some extension to really solve the problem. I wrote the following to solve a similar problem. The find_WeekdaysFromThisTo() method is brute-force, but it works reasonably quickly if your time span is less than 2 years.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement