Advertisement
MrPauloeN

Obliczanie aktualnego wieku od podanej daty WP ShortCode

Mar 17th, 2019 (edited)
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.84 KB | None | 0 0
  1. /*
  2.  * Fukcja obliczająca ilość lat od podanej daty
  3.  * Licence: CC-BY-SA 4.0
  4.  * @author: Paweł Nowak
  5.  *
  6.  * @param array $wiek {
  7.  *     Attributes of the age shortcode.
  8.  *
  9.  * @type string     $rok        Data, od której obliczane są lata.
  10.  * @type string     $format     Format wywietlania wieku. Format musi pasować do wprowadzonej daty.
  11.  *                              Więcej tu:
  12.  *                              @link: http://php.net/manual/en/datetime.createfromformat.php
  13.  *
  14.  * @return string   @wynik      Zwraca obliczoną ilość lat od podanej daty w @type @rok
  15.  **/
  16.  
  17. add_shortcode('wiek', 'wpsolucje__sc__wiek');
  18. function wpsolucje__sc__wiek( $wiek ){
  19.    
  20.     $wiek = shortcode_atts( array(
  21.         'rok' => '2000',
  22.         'format' => 'Y',
  23.     ), $wiek );
  24.    
  25.    
  26.     $rok = DateTime::createFromFormat( 'Y', $wiek['rok'] );
  27.     $teraz = new DateTime();
  28.     $wynik = $teraz->diff( $rok );
  29.      
  30.     return $wynik->y;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement