Posted By

manavecplan on 09/12/10


Tagged


Versions (?)

Age shortcode


 / Published in: PHP
 

  1. function calc_my_age() {
  2. // define the dates
  3. $now=strtotime("now");
  4. $birthdate=strtotime("24 August 1966");
  5. $lengths = array("60","60","24","7","4.35","12","10");
  6.  
  7. $difference = $now - $birthdate;
  8.  
  9. for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
  10. $difference /= $lengths[$j];
  11. }
  12.  
  13. $difference = intval($difference);
  14.  
  15.  
  16. return $difference;
  17.  
  18. }
  19.  
  20. add_shortcode('peter-age','calc_my_age');
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31. function calc_my_age($atts) {
  32.  
  33. // give $atts a default value
  34. extract(shortcode_atts(array(
  35. 'modifier' => "0",
  36. ), $atts));
  37.  
  38.  
  39. // define the dates
  40. $now=strtotime("now");
  41. $birthdate=strtotime("24 August 1966");
  42. $lengths = array("60","60","24","7","4.35","12","10");
  43.  
  44. $difference = $now - $birthdate;
  45.  
  46. for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
  47. $difference /= $lengths[$j];
  48. }
  49.  
  50. $difference = intval($difference);
  51.  
  52. $difference = $difference + (int)$modifier;
  53.  
  54. return $difference;
  55.  
  56. }
  57.  
  58. add_shortcode('peter-age','calc_my_age');

Report this snippet  

You need to login to post a comment.