get the number of weekends between two dates

Discussion in 'PHP' started by cleidomonscole, Oct 16, 2008.

  1. #1
    imagene that u have two dates, example:

    $inicio ="2008-10-10";
    $fim ="2008-10-16";


    I need a function that when I insert the two dates, the funcions returns the number of weekends that exist between the two date, I would be very greatfull for the help , tks
     
    cleidomonscole, Oct 16, 2008 IP
  2. webrickco

    webrickco Active Member

    Messages:
    268
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Just a tip if I may:
    1 - Transform both dates into Unix timestamps (difference in seconds from January, 1st of 1970), like this:
      
    $difinicio=strtotime($inicio.' 00:00:00');
    $diffim=strtotime($fim.' 00:00:00');
    PHP:
    2 - check if $difinicio is a Sunday. If it is, add one to $weekcounter (counting sundays is the same as counting weekends). A Sunday is when date("w", $difinicio) == 0
    
    if (date("w", $difinicio) == 0)
    {
      $weekcounter++; 
    }
    
    PHP:

    3 - Add one day to $difinicio like this:
    
    $difinicio=  strtotime('+1 day',strtotime(date("Y",$difinicio).'-'.date("m",$difinicio).'-'.date("d",$difinicio).' 00:00:00'));
    
    PHP:
    4 - Repeat step 2 and 3 until $difinicio = $diffim

    5 - $weekcounter should be your result.

    This should work and I hope this helps.
    You're a portuguese right? boa sorte then!
     
    webrickco, Oct 16, 2008 IP
  3. cleidomonscole

    cleidomonscole Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thank u so much, u dont know how much u helped me, ya Im portuguese. :)
     
    cleidomonscole, Oct 16, 2008 IP