Subtract number of days from date in url parameter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cptuser
    New Member
    • Mar 2007
    • 30

    Subtract number of days from date in url parameter

    Hi,
    I'm trying to subtract 6 days from a date in a url parameter (date_param). For example, "http://localhost/somepage?date_p aram=16/08/07".

    This is the code that I have, but it's giving the wrong value. Can someone help?
    I'm new to PHP.

    Code:
     <?php $date = $_REQUEST["date_param"];
    echo date('d/m/y', strtotime($date.' -6 days')) ?>
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, CPT.

    Try using strtotime() on the parameter, and then subtracting the number of seconds in 6 days.

    Comment

    • cptuser
      New Member
      • Mar 2007
      • 30

      #3
      Originally posted by pbmods
      Heya, CPT.

      Try using strtotime() on the parameter, and then subtracting the number of seconds in 6 days.
      Could you please provide me with an example code. I'm very new to PHP.

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, CPT.

        So you've got yourself some code:
        [code=php]
        $date = $_REQUEST['date_param'];
        [/code]

        We can use this to find the current time:
        [code=php]
        $date = strtotime($_REQ UEST['date_param']);
        [/code]

        And then to find six days before that:
        [code=php]
        $date = strtotime($_REQ UEST['date_param']);
        $date -= (6 * 24 * 60 * 60); // 6 days * 24 hours/day * 60 minutes/hour * 60 seconds/minute.

        // Or, for maximum efficiency:
        $date -= 518400;
        [/code]

        The value returned by strtotime() represents the number of seconds since December 31, 1969 (for various reasons).

        Comment

        • cptuser
          New Member
          • Mar 2007
          • 30

          #5
          Fantastic! it worked perfectly. I just had to add the last bit which converts it back to date format and not a timestamp. Here is my final code. the url param is "date". Let me know if there is a shorter way of doing it??

          [PHP]$timeEnd = strtotime($_REQ UEST['date']); //Changes the date int he URL to linux time stamp. URL param must be yyyy/mm/dd
          $timeMon = $timeEnd - 518400; // 6 days * 24 hours/day * 60 minutes/hour * 60 seconds/minute.
          $dateMon = date('d/m/Y', $timeMon); // converts the time stamp back to date format[/PHP]

          Comment

          • pbmods
            Recognized Expert Expert
            • Apr 2007
            • 5821

            #6
            Heya, CPT.

            Looks good. You could potentially save yourself a few nybbles of memory, and 2 or three CPU cycles by doing it all with one variable:
            [code=php]
            $dameMon = date('d/m/Y', strtotime($_REQ UEST['date']) - 518400);
            [/code]

            That one's an issue of whatever is most comfortable for you, and PHP doesn't really mind one way or the other.

            Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)

            Comment

            Working...