Results 1 to 6 of 6

Thread: Custom function gone crazy.

  1. #1
    Join Date
    Oct 2007
    Posts
    53
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default Custom function gone crazy.

    I am sure I have made a relatively stupid error, a helping hand would be great!

    My code:
    PHP Code:
    <?php
    // Returns an acurate number of seconds in the current month.
    function months_seconds() {
    if(
    date(n) = 1) { // January.
      
    $jan date(n) * 31 24 60 60;
      return(
    $jan);
    } elseif(
    date(n) = 2) { // February.
      
    if(date(L) = 1) { // Leap year.
        
    $lfeb date(n) * 29 24 60 60;
        return(
    $lfeb);
      } elseif(
    date(L) = 0) { // No leap year.
        
    $nfeb date(n) * 28 24 60 60;
        return(
    $nfeb);
      }
    } elseif(
    date(n) = 3) { // March.
      
    $mar date(n) * 31 24 60 60;
      return(
    $mar);
    } elseif(
    date(n) = 4) { // April.
      
    $apr date(n) * 30 24 60 60;
      return(
    $apr);
    } elseif(
    date(n) = 5) { // May.
      
    $may date(n) * 31 24 60 60;
      return(
    $may);
    } elseif(
    date(n) = 6) { // June.
      
    $jun date(n) * 30 24 60 60;
      return(
    $jun);
    } elseif(
    date(n) = 7) { // July.
      
    $jul date(n) * 31 24 60 60;
      return(
    $jul);
    } elseif(
    date(n) = 8) { // August.
      
    $aug date(n) * 31 24 60 60;
      return(
    $aug);
    } elseif(
    date(n) = 9) { // September.
      
    $sep date(n) * 30 24 60 60;
      return(
    $sep);
    } elseif(
    date(n) = 10) { //October.
      
    $oct date(n) * 31 24 60 60;
      return(
    $oct);
    } elseif(
    date(n) = 11) { // November.
      
    $nov date(n) * 30 24 60 60;
      return(
    $nov);
    } elseif(
    date(n) = 12) { // December.
      
    $dec date(n) * 31 24 60 60;
      return(
    $dec);
    }
    }
    echo(
    months_seconds());
    ?>
    The error: "Fatal error: Can't use function return value in write context in /home/shadowte/public_html/months_seconds.php on line 4"

    This simple function gets the month of the year, then converts that into seconds.

    As I said, im sure I have made some stupid error in my attempt to simplify the process of getting the current month's number of seconds. Any help would be greatly appreciated.

  2. #2
    Join Date
    Nov 2007
    Location
    USA
    Posts
    170
    Thanks
    8
    Thanked 22 Times in 22 Posts

    Default

    not sure if this will help you out on your error but you should probably change all of your "=" in your ifs and else ifs to "=="...

  3. The Following User Says Thank You to Moshambi For This Useful Post:

    AmenKa (09-16-2008)

  4. #3
    Join Date
    Oct 2007
    Posts
    53
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default

    We have a winner! Thank you! Funny how those stupid little things kill you when you aren't sharp.

  5. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    *puts a define('L', 2500) at the top of your script and watches the function break*

    By default, PHP will convert undefined constants like L and n in that script to strings. However. you must never, ever rely upon this. Put them in quotes.

    That's one redundant function. It can be better written so:
    Code:
    function seconds_in_month($time = false) {
      if ($time === false)
        $time = time();
    
      list($y, $m) = explode('-', date('Y-m', $time));
      return strtotime(sprintf('%s-%s-01', $y, $m + 1)) - strtotime(sprintf('%s-%s-01', $y, $m));
    }
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. The Following User Says Thank You to Twey For This Useful Post:

    AmenKa (09-16-2008)

  7. #5
    Join Date
    Nov 2007
    Location
    USA
    Posts
    170
    Thanks
    8
    Thanked 22 Times in 22 Posts

    Default

    Quote Originally Posted by AmenKa View Post
    We have a winner! Thank you! Funny how those stupid little things kill you when you aren't sharp.
    Lol I wasn't sure if that was gonna fix your problem but I'm glad it did!

  8. #6
    Join Date
    Oct 2007
    Posts
    53
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default

    Quote Originally Posted by Twey View Post
    *puts a define('L', 2500) at the top of your script and watches the function break*

    By default, PHP will convert undefined constants like L and n in that script to strings. However. you must never, ever rely upon this. Put them in quotes.

    That's one redundant function. It can be better written so:
    Code:
    function seconds_in_month($time = false) {
      if ($time === false)
        $time = time();
    
      list($y, $m) = explode('-', date('Y-m', $time));
      return strtotime(sprintf('%s-%s-01', $y, $m + 1)) - strtotime(sprintf('%s-%s-01', $y, $m));
    }
    I was actually wondering yesterday if that mattered, good to have it cleared up.

Bookmarks

Posting Permissions

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