Need help in PHP date functioning

Discussion in 'PHP' started by Magina™, May 24, 2011.

  1. #1
    Greetings!

    I have this problem encountered and I need to finish this as my home work. My assignment is all about date functioning. I need to get the number of weeks from the specific date to current date. Using the database MySQL.

    For example:

    My database MySQL has from and to column.

    from: 2011-05-24
    to: 2011-07-24

    that is for the database record. Now my assignment in PHP part is to calculate how many weeks are their in that range given. I hope you can help me guys. I am totally new to PHP and still learning. I hopr you can help me with this.

    Thank you,
    Magina
     
    Magina™, May 24, 2011 IP
  2. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #2
    You could do this within the actual MySQL query, but if you're after a PHP, this should work:

    First convert the dates to a unix timestamp, minus the start date from the end date and then divide that by the seconds in a week (unix timestamps are based on seconds) and then round the number given.

    
    <?php
    $start = strtotime('24-04-2011');
    $end   = strtotime('24-05-2011');
    
    echo round(($end - $start) / 604800);
    
    PHP:
     
    crazyryan, May 24, 2011 IP