Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 59361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:59:35+00:00 2026-05-10T17:59:35+00:00

How do you actually perform datetime operations such as adding date, finding difference, find

  • 0

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?

SELECT  COUNT(*) AS total_days FROM    (SELECT date '2008-8-26' + generate_series(0,           (date '2008-9-1' - date '2008-8-26')) AS all_days) AS calendar WHERE   EXTRACT(isodow FROM all_days) < 6; 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T17:59:36+00:00Added an answer on May 10, 2026 at 5:59 pm

    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.

    $tryme = new Extended_DateTime('2007-8-26'); $newer = new Extended_DateTime('2008-9-1');  print 'Weekdays From '.$tryme->format('Y-m-d').' To '.$newer->format('Y-m-d').': '.$tryme -> find_WeekdaysFromThisTo($newer) .'\n'; /*  Output:  Weekdays From 2007-08-26 To 2008-09-01: 265  */ print 'All Days From '.$tryme->format('Y-m-d').' To '.$newer->format('Y-m-d').': '.$tryme -> find_AllDaysFromThisTo($newer) .'\n'; /*  Output:  All Days From 2007-08-26 To 2008-09-01: 371   */ $timefrom = $tryme->find_TimeFromThisTo($newer); print 'Between '.$tryme->format('Y-m-d').' and '.$newer->format('Y-m-d').' there are '.       $timefrom['years'].' years, '.$timefrom['months'].' months, and '.$timefrom['days'].       ' days.'.'\n'; /*  Output: Between 2007-08-26 and 2008-09-01 there are 1 years, 0 months, and 5 days. */  class Extended_DateTime extends DateTime {      public function find_TimeFromThisTo($newer) {         $timefrom = array('years'=>0,'months'=>0,'days'=>0);          // Clone because we're using modify(), which will destroy the object that was passed in by reference         $testnewer = clone $newer;          $timefrom['years'] = $this->find_YearsFromThisTo($testnewer);         $mod = '-'.$timefrom['years'].' years';         $testnewer -> modify($mod);          $timefrom['months'] = $this->find_MonthsFromThisTo($testnewer);         $mod = '-'.$timefrom['months'].' months';         $testnewer -> modify($mod);          $timefrom['days'] = $this->find_AllDaysFromThisTo($testnewer);         return $timefrom;     } // end function find_TimeFromThisTo       public function find_YearsFromThisTo($newer) {         /*         If the passed is:         not an object, not of class DateTime or one of its children,         or not larger (after) $this         return false         */         if (!is_object($newer) || !($newer instanceof DateTime) || $newer->format('U') < $this->format('U'))             return FALSE;         $count = 0;          // Clone because we're using modify(), which will destroy the object that was passed in by reference         $testnewer = clone $newer;          $testnewer -> modify ('-1 year');         while ( $this->format('U') < $testnewer->format('U')) {             $count ++;             $testnewer -> modify ('-1 year');         }         return $count;     } // end function find_YearsFromThisTo       public function find_MonthsFromThisTo($newer) {         /*         If the passed is:         not an object, not of class DateTime or one of its children,         or not larger (after) $this         return false         */         if (!is_object($newer) || !($newer instanceof DateTime) || $newer->format('U') < $this->format('U'))             return FALSE;          $count = 0;         // Clone because we're using modify(), which will destroy the object that was passed in by reference         $testnewer = clone $newer;         $testnewer -> modify ('-1 month');          while ( $this->format('U') < $testnewer->format('U')) {             $count ++;             $testnewer -> modify ('-1 month');         }         return $count;     } // end function find_MonthsFromThisTo       public function find_AllDaysFromThisTo($newer) {         /*         If the passed is:         not an object, not of class DateTime or one of its children,         or not larger (after) $this         return false         */         if (!is_object($newer) || !($newer instanceof DateTime) || $newer->format('U') < $this->format('U'))             return FALSE;          $count = 0;         // Clone because we're using modify(), which will destroy the object that was passed in by reference         $testnewer = clone $newer;         $testnewer -> modify ('-1 day');          while ( $this->format('U') < $testnewer->format('U')) {             $count ++;             $testnewer -> modify ('-1 day');         }         return $count;     } // end function find_AllDaysFromThisTo       public function find_WeekdaysFromThisTo($newer) {         /*         If the passed is:         not an object, not of class DateTime or one of its children,         or not larger (after) $this         return false         */         if (!is_object($newer) || !($newer instanceof DateTime) || $newer->format('U') < $this->format('U'))             return FALSE;          $count = 0;          // Clone because we're using modify(), which will destroy the object that was passed in by reference         $testnewer = clone $newer;         $testnewer -> modify ('-1 day');          while ( $this->format('U') < $testnewer->format('U')) {             // If the calculated day is not Sunday or Saturday, count this day             if ($testnewer->format('w') != '0' && $testnewer->format('w') != '6')                 $count ++;             $testnewer -> modify ('-1 day');         }         return $count;     } // end function find_WeekdaysFromThisTo      public function set_Day($newday) {         if (is_int($newday) && $newday > 0 && $newday < 32 && checkdate($this->format('m'),$newday,$this->format('Y')))             $this->setDate($this->format('Y'),$this->format('m'),$newday);     } // end function set_Day       public function set_Month($newmonth) {         if (is_int($newmonth) && $newmonth > 0 && $newmonth < 13)             $this->setDate($this->format('Y'),$newmonth,$this->format('d'));     } // end function set_Month       public function set_Year($newyear) {         if (is_int($newyear) && $newyear > 0)             $this->setDate($newyear,$this->format('m'),$this->format('d'));     } // end function set_Year } // end class Extended_DateTime 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 66k
  • Answers 66k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Use spatial extensions, most databases have this. In MySql you… May 11, 2026 at 11:38 am
  • added an answer Rather than adding a parameter to every call in the… May 11, 2026 at 11:38 am
  • added an answer Steve My suggestion is, if you are able, to use… May 11, 2026 at 11:38 am

Related Questions

How do you actually perform datetime operations such as adding date, finding difference, find
How do you do low low level sockets in C, example: actually sending a
How do you update this? I've never seen any current team that actually checks
How do you test the usability of the user interfaces of your applications -
How do you create your own custom moniker (or URL Protocol) on Windows systems?
How do you restore a database backup using SQL Server 2005 over the network?
How do you retrieve selected text using Regex in C#? I am looking for
How do you create a static class in C++? I should be able to
How do you configure tomcat to bind to a single ip address (localhost) instead
How do you expose a LINQ query as an ASMX web service? Usually, from

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.