krokos.net.pl
Masz wyłączoną obsługę javascript, niektóre funkcje na stronie mogą działać nieprawidłowo
20.04.2024
Agnieszka, Amalia, Czech, Czesław, Czcibor, Jagna, Marcelina
dziś jest : 111 dzień roku wschód słońca o : 5:42 , zachód o : 19:45 koniec roku za : 254 dni
Programowanie PHP - Różnica Czasu Pomiędzy Wyznaczonymi Datami (Odliczanie Od ... / Do ...)
Klasa pozwala na obliczenie różnicy pomiędzy podanymi datami, zarówno od daty ..., oraz do daty .... W jednym przypadku będzie to odliczanie wstecz, a w drugim do określonej daty. Czas podawany jest w formie : lat, miesięcy, tygodni, dni, godzin, minut i sekund.
CODE : php
  1. /**
  2.  * the difference between two dates
  3.  * copyright : (c) 2013 Andrzej Krokos
  4.  * version PHP : 5
  5.  * author : shreker983@gmail.com
  6.  * licence : GPL
  7.  *
  8. */
  9.  
  10. if(function_exists('date_default_timezone_set'))
  11. {
  12. }
  13.  
  14. class dtimediff
  15. {
  16.     // set data
  17.     function setdata($status, $autorev, $showsec, $showday, $df, $dt, $mess)
  18.     {
  19.         $this->status = $status;
  20.         $this->autorev = $autorev;
  21.         $this->showsec = $showsec;
  22.         $this->showday = $showday;
  23.         ($df != '') ? $this->data_from = $df : $this->data_from = date('Y-m-d H:i:s');
  24.         ($dt != '') ? $this->data_to = $dt : $this->data_to = date('Y-m-d H:i:s');
  25.         $this->font = 'font-family:verdana; color:black; font-size:13px; font-weight:normal;';
  26.         $this->mess = $mess;
  27.     }
  28.     // manipulation data
  29.     function diff()
  30.     {
  31.         if ($this->status != false)
  32.         {
  33.             $this->tc = $this->data_to;
  34.             if (strlen($this->data_to) == 19)
  35.                 $this->data_to = $this->data_to;
  36.             else    
  37.                 $this->data_to = $this->data_to.' 00:00:00';  
  38.                          
  39.             if (!is_int($this->data_from))
  40.             {
  41.                 $this->sdata_from = strtotime($this->data_from);
  42.             }
  43.  
  44.             if (!is_int($this->data_to))
  45.             {
  46.                 $this->sdata_to = strtotime($this->data_to);
  47.             }
  48.  
  49.             $gd_from = getdate($this->sdata_from);
  50.             $gd_to = getdate($this->sdata_to);
  51.             $this->sumdays = round(abs(mktime(0, 0, 0, $gd_from['mon'], $gd_from['mday'], $gd_from['year']) - mktime(0, 0, 0, $gd_to['mon'], $gd_to['mday'], $gd_to['year'])) / 86400);
  52.  
  53.             if ($this->sdata_from > $this->sdata_to and $this->autorev == true)
  54.             {
  55.                 $this->ttime = $this->sdata_from;
  56.                 $this->sdata_from = $this->sdata_to;
  57.                 $this->sdata_to = $this->ttime;
  58.             }
  59.          
  60.             $intervals = array('year', 'month', 'day', 'hour', 'minute', 'second');
  61.             $diffs = array();
  62.  
  63.             foreach ($intervals as $interval)
  64.             {
  65.                 $diffs[$interval] = 0;
  66.                 $this->ttime = strtotime('+1 '.$interval, $this->sdata_from);
  67.                 while ($this->sdata_to >= $this->ttime)
  68.                 {
  69.                     $this->sdata_from = $this->ttime;
  70.                     $diffs[$interval]++;
  71.                     $this->ttime = strtotime('+1 '.$interval, $this->sdata_from);
  72.                 }
  73.             }
  74.    
  75.             $this->years = ($diffs['year'] < 10) ? '0'.$diffs['year'] : $diffs['year'];
  76.             $this->months = ($diffs['month'] < 10) ? '0'.$diffs['month'] : $diffs['month'];
  77.             $this->days = ($diffs['day'] < 10) ? '0'.$diffs['day'] : $diffs['day'];
  78.             $this->hours = ($diffs['hour'] < 10) ? '0'.$diffs['hour'] : $diffs['hour'];
  79.             $this->minuts = ($diffs['minute'] < 10) ? '0'.$diffs['minute'] : $diffs['minute'];
  80.             $this->seconds = ($diffs['second'] < 10) ? '0'.$diffs['second'] : $diffs['second'];
  81.  
  82.             if ($this->years == 0)
  83.                 $this->tyears = '';
  84.             else if ($this->years == 1)
  85.                 $this->tyears = 'rok';
  86.             else if ((($this->years > 1) && ($this->years < 5)) || (($this->years > 20) && (($this->years % 10 > 1) && ($this->years % 10 < 5))))
  87.                 $this->tyears = 'lata';
  88.             else
  89.                 $this->tyears = 'lat';
  90.         }
  91.         else
  92.         {
  93.             // no reaction
  94.         }
  95.     }
  96.     // show data
  97.     function display()
  98.     {  
  99.         if ($this->status != false)
  100.         {
  101.             if (strtotime('now') < strtotime($this->tc) and $this->autorev != true)
  102.             {
  103.                 echo ($this->days > 1) ? '<font style="'.$this->font.'"><strong>'.$this->mess['1'].'</strong>'.(($this->years > 0) ? '<strong>'.$this->years.'</strong>' : '').'<small> '.$this->tyears.' </small>'.(($this->months > 0) ? '<strong>'.$this->months.'</strong>' : '').(($this->months > 0) ? ' <small> mies. </small> ' : '').(($this->days > 0) ? '<strong>'.$this->days.'</strong><small>'.(($this->days == 1) ? ' dzień' : ' dni').'</small>' : '').' '.(($this->hours > 0) ? '<strong>'.$this->hours.'</strong>' : '').(($this->hours > 0) ? ' <small> godz. </small> ' : '').(($this->minuts > 0) ? '<strong>'.$this->minuts.'</strong>' : '').(($this->minuts > 0) ? ' <small> min. </small> ' : '').(($this->showsec == true) ? (($this->seconds > 0) ? '<strong>'.$this->seconds.'</strong>' : '').(($this->seconds > 0) ? ' <small> sec. </small> ' : '') : '').(($this->showday == 1) ? '(<strong>'.$this->sumdays.'</strong>'.(($this->sumdays > 1) ? ' dni' : ' dzień').')' : '').'</font>' : '<font style="'.$this->font.'"><strong>'.$this->mess['2'].'</strong>'.(($this->years > 0) ? '<strong>'.$this->years.'</strong>' : '').'<small> '.$this->tyears.' </small>'.(($this->months > 0) ? '<strong>'.$this->months.'</strong>' : '').(($this->months > 0) ? ' <small> mies. </small> ' : '').(($this->days > 0) ? '<strong>'.$this->days.'</strong><small>'.(($this->days == 1) ? ' dzień' : ' dni').'</small>' : '').' '.(($this->hours > 0) ? '<strong>'.$this->hours.'</strong>' : '').(($this->hours > 0) ? ' <small> godz. </small> ' : '').(($this->minuts > 0) ? '<strong>'.$this->minuts.'</strong>' : '').(($this->minuts > 0) ? ' <small> min. </small> ' : '').(($this->showsec == true) ? (($this->seconds > 0) ? '<strong>'.$this->seconds.'</strong>' : '').(($this->seconds > 0) ? ' <small> sec. </small> ' : '') : '').(($this->showday == 1) ? '(<strong>'.$this->sumdays.'</strong>'.(($this->sumdays > 1) ? ' dni' : ' dzień').')' : '').'<strong>'.$this->mess['3'].'</strong>'.'</font>';
  104.             }
  105.             else if (strtotime('now') > strtotime($this->tc) and $this->autorev == true)
  106.             {
  107.                 echo ($this->days > 1) ? '<font style="'.$this->font.'"><strong>'.$this->mess['5'].'</strong>'.(($this->years > 0) ? '<strong>'.$this->years.'</strong>' : '').'<small> '.$this->tyears.' </small>'.(($this->months > 0) ? '<strong>'.$this->months.'</strong>' : '').(($this->months > 0) ? ' <small> mies. </small> ' : '').(($this->days > 0) ? '<strong>'.$this->days.'</strong><small>'.(($this->days == 1) ? ' dzień' : ' dni').'</small>' : '').' '.(($this->hours > 0) ? '<strong>'.$this->hours.'</strong>' : '').(($this->hours > 0) ? ' <small> godz. </small> ' : '').(($this->minuts > 0) ? '<strong>'.$this->minuts.'</strong>' : '').(($this->minuts > 0) ? ' <small> min. </small> ' : '').(($this->showsec == true) ? (($this->seconds > 0) ? '<strong>'.$this->seconds.'</strong>' : '').(($this->seconds > 0) ? ' <small> sec. </small> ' : '') : '').(($this->showday == 1) ? '(<strong>'.$this->sumdays.'</strong>'.(($this->sumdays > 1) ? ' dni' : ' dzień').')' : '').'</font>' : '<font style="'.$this->font.'"><strong>'.$this->mess['5'].'</strong>'.(($this->years > 0) ? '<strong>'.$this->years.'</strong>' : '').'<small> '.$this->tyears.' </small>'.(($this->months > 0) ? '<strong>'.$this->months.'</strong>' : '').(($this->months > 0) ? ' <small> mies. </small> ' : '').(($this->days > 0) ? '<strong>'.$this->days.'</strong><small>'.(($this->days == 1) ? ' dzień' : ' dni').'</small>' : '').' '.(($this->hours > 0) ? '<strong>'.$this->hours.'</strong>' : '').(($this->hours > 0) ? ' <small> godz. </small> ' : '').(($this->minuts > 0) ? '<strong>'.$this->minuts.'</strong>' : '').(($this->minuts > 0) ? ' <small> min. </small> ' : '').(($this->showsec == true) ? (($this->seconds > 0) ? '<strong>'.$this->seconds.'</strong>' : '').(($this->seconds > 0) ? ' <small> sec. </small> ' : '') : '').(($this->showday == 1) ? '(<strong>'.$this->sumdays.'</strong>'.(($this->sumdays > 1) ? ' dni' : ' dzień').')' : '').'<strong>'.$this->mess['6'].'</strong>'.'</font>';
  108.             }
  109.             else
  110.             {
  111.                 echo '<font style="'.$this->font.'"><strong>'.$this->mess['4'].'</strong>'.'</font>';
  112.             }
  113.         }
  114.         else
  115.         {
  116.             // no reaction
  117.         }
  118.     }
  119. }
przykład :
CODE : php
  1. $t = new dtimediff();
  2. // status, autoreverse, show seconds, show days, start, end, name event (array)
  3. $t->setdata(1, 0, 1, 1, date('Y-m-d H:i:s'), '2015-04-12 00:00:00', array('', 'wydarzenie za : ', 'pozostało : ', ' do wydarzenia', 'koniec odliczania', 'minęło : ', ' od wydarzenia'));
  4. $t->diff();
  5. $t->display();
pobierz plik ( rozmiar : 1.71 kB )
ocena : 4.9(oddano : 10 głosów)
online : 1 użytkownik, dziś odwiedziło : 14 osób
Zgodnie z nowelizacją ustawy o Prawie Telekomunikacyjnym informujemy, że strona krokos.net.pl w swoim działaniu korzysta z zapisywanych informacji w postaci ciasteczek (ang. cookies).
Szlachetna Paczka
powered by scms © 2004 - 2024 design by sid