Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    May 2004
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Last Monday's Date Function

    I have this function to work out the date of the monday just past.

    Function is called like this
    PHP Code:
    $time time(); 
    PHP Code:
    function get_monday($time) {
      
    $currday date("D"mktime(substr($time,6,2)));
      if (
    $currday == "Mon") {
        
    $new_time time();
        
    $monday date("d m Y"mktime(substr($new_time,6,2),substr($new_time,4,2),substr($new_time,0,4)));
        echo 
    $monday;
      }
      else if (
    $currday == "Tue") {
        
    $new_time = (time() - 86400);
        
    $monday date("d m Y"mktime(substr($new_time,6,2),substr($new_time,4,2),substr($new_time,0,4)));
        echo 
    $monday;
      }
      else if (
    $currday == "Wed") {
        
    $new_time = (time() - 172800);
        
    $monday date("d m Y"mktime(substr($new_time,6,2),substr($new_time,4,2),substr($new_time,0,4)));
        echo 
    $monday;
      }
      else if (
    $currday == "Thu") {
        
    $new_time = (time() - 259200);
        
    $monday date("d m Y"mktime(substr($new_time,6,2),substr($new_time,4,2),substr($new_time,0,4)));
        echo 
    $monday;
      }
      else if (
    $currday == "Fri") {
        
    $new_time = (time() - 345600);
        
    $monday date("d m Y"mktime(substr($new_time,6,2),substr($new_time,4,2),substr($new_time,0,4)));
        echo 
    $monday;
      }

    However when I test it, the variable $monday is set to a day in the future not in the past. Any help on getting the correct date would be greatly appreciated, as would any advice on reducing the number of lines in the function.
    "The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect."

    --Tim Berners-Lee, W3C Director and Inventor of the World Wide Web

  • #2
    New Coder
    Join Date
    Jun 2004
    Location
    Newcastle, England
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts
    http://uk2.php.net/manual/en/function.strtotime.php

    This function already does it:

    eg.

    echo strtotime("last Monday");
    Beauty is in the eye of the beer-holder

    Man who smoke pot, choke on handle

  • #3
    New Coder
    Join Date
    May 2004
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts
    he he he... seems theres a lot of PHP functions I still need to learn!

    Many thanks!
    "The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect."

    --Tim Berners-Lee, W3C Director and Inventor of the World Wide Web


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •