We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 15136
    • 34 Posts
    Ok, I'm experiencing some weird behavior.

    Snippet code:
      
    
    echo $dob; // this was passed in via snippet call parameter and Outputs "1962-04-23" correctly.
    echo gettype($dob); // it is in fact a string ;)
    
    $dob2="1962-04-23";
    echo gettype($dob2); // it is in fact a string as well ;)
    
    //So in theory $dob and $dob2 are the exact same thing with the exact same value, however when I //use $dob below (snippet param), it outputs 2013 (wrong) but when I use $dob2 (hard coded above) it outputs //"50" like it is supposed to.
    
    $y = explode("-", $dob);		
    			$year = date("Y") - $y[0];
    			if (date("m") >= $y[1])
    				if (date("d") >= $y[2])
    					$year++;
    return $year;  
    
    


    Resource Code:

    
    [[formatAge? &dob=`[[+dob:date=`%Y-%m-%d`]]`]]
    
      


    I'm a mediocre PHP'er at best so this has me scratching my head. Any ideas?
      2.2.4 Revolution
      • 3749
      • 24,544 Posts
      If $dob is a unix timestamp (as it is when you get it from the User Profile), this will calculate the age:

      $age = intval(substr(date('Ymd') - date('Ymd', $dob), 0, -4));


      If $dob is a string containing the date, use this:

      $age = intval(substr(date('Ymd') - date('Ymd', strtotime($dob)), 0, -4));



      Or, a more generic version:

      if (is_string($dob)) {
          $age = intval(substr(date('Ymd') - date('Ymd', strtotime($dob)), 0, -4));
      } else {
          $age = intval(substr(date('Ymd') - date('Ymd', $dob), 0, -4));
      }
      




      ------------------------------------------------------------------------------------------
      PLEASE, PLEASE specify the version of MODX you are using.
      MODX info for everyone: http://bobsguides.com/modx.html [ed. note: BobRay last edited this post 14 years, 2 months ago.]
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 15136
        • 34 Posts
        well now whenever I send it in as a string on an integer the output is the same, but the output is 42 lol. Regardless of the any date set, the answer is always 42.

         
        
        echo "dob = $dob"; // returns 1983-10-20 or 435456000 depending on my resource call 
        if (is_string($dob)) {
            $age = intval(substr(date('Ymd') - date('Ymd', strtotime($dob)), 0, -4));
        } else {
            $age = intval(substr(date('Ymd') - date('Ymd', $dob), 0, -4));
        }
        echo "   now the dob = ";
        return $age;  // returns 42, if i change the date, the answer is still 42.
        
        
          2.2.4 Revolution
          • 3749
          • 24,544 Posts
          Call the snippet uncached.

          [[!SnippetName]]



          ------------------------------------------------------------------------------------------
          PLEASE, PLEASE specify the version of MODX you are using.
          MODX info for everyone: http://bobsguides.com/modx.html
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 15136
            • 34 Posts
            works perfect thanks!
              2.2.4 Revolution
              • 3749
              • 24,544 Posts
              I'm glad it worked for you. Thanks for reporting back. smiley


              ------------------------------------------------------------------------------------------
              PLEASE, PLEASE specify the version of MODX you are using.
              MODX info for everyone: http://bobsguides.com/modx.html
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting