18.02.2025
Konstancja, Krystian, Sylwana, Szymon, Albert, Fryda, Zuzanna
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.
WSPARCIE / FORUM
CODE : php
/**
* the difference between two dates
* copyright : (c) 2013 Andrzej Krokos
* version PHP : 5
* author : shreker983@gmail.com
* licence : GPL
*
*/
{
}
class dtimediff
{
// set data
function setdata( $status , $autorev , $showsec , $showday , $df , $dt , $mess )
{
$this -> status = $status ;
$this -> autorev = $autorev ;
$this -> showsec = $showsec ;
$this -> showday = $showday ;
( $df != '' ) ?
$this -> data_from = $df : $this -> data_from = date ( 'Y-m-d H:i:s' ) ;
( $dt != '' ) ?
$this -> data_to = $dt : $this -> data_to = date ( 'Y-m-d H:i:s' ) ;
$this -> font = 'font-family:verdana; color:black; font-size:13px; font-weight:normal;' ;
$this -> mess = $mess ;
}
// manipulation data
function diff( )
{
if ( $this -> status != false )
{
$this -> tc = $this -> data_to ;
if ( strlen ( $this -> data_to ) == 19
)
$this -> data_to = $this -> data_to ;
else
$this -> data_to = $this -> data_to . ' 00:00:00' ;
if ( ! is_int ( $this -> data_from ) )
{
$this -> sdata_from = strtotime ( $this -> data_from ) ;
}
{
}
$gd_from = getdate ( $this -> sdata_from ) ;
$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
) ;
if ( $this -> sdata_from > $this -> sdata_to and $this -> autorev == true )
{
$this -> ttime = $this -> sdata_from ;
$this -> sdata_from = $this -> sdata_to ;
$this -> sdata_to = $this -> ttime ;
}
$intervals = array ( 'year' , 'month' , 'day' , 'hour' , 'minute' , 'second' ) ;
foreach ( $intervals as $interval )
{
$diffs [ $interval ] = 0;
$this -> ttime = strtotime ( '+1 ' . $interval , $this -> sdata_from ) ;
while ( $this -> sdata_to >= $this -> ttime )
{
$this -> sdata_from = $this -> ttime ;
$diffs [ $interval ] ++;
$this -> ttime = strtotime ( '+1 ' . $interval , $this -> sdata_from ) ;
}
}
$this -> years = ( $diffs [ 'year' ] < 10) ? '0' . $diffs [ 'year' ] : $diffs [ 'year' ] ;
$this -> months = ( $diffs [ 'month' ] < 10) ? '0' . $diffs [ 'month' ] : $diffs [ 'month' ] ;
$this -> days = ( $diffs [ 'day' ] < 10) ? '0' . $diffs [ 'day' ] : $diffs [ 'day' ] ;
$this -> hours = ( $diffs [ 'hour' ] < 10) ? '0' . $diffs [ 'hour' ] : $diffs [ 'hour' ] ;
$this -> minuts = ( $diffs [ 'minute' ] < 10) ? '0' . $diffs [ 'minute' ] : $diffs [ 'minute' ] ;
$this -> seconds = ( $diffs [ 'second' ] < 10) ? '0' . $diffs [ 'second' ] : $diffs [ 'second' ] ;
if ( $this -> years == 0)
$this -> tyears = '' ;
else if ( $this -> years == 1)
$this -> tyears = 'rok' ;
else if ( ( ( $this -> years > 1) && ( $this -> years < 5) ) || ( ( $this -> years > 20) && ( ( $this -> years % 10 > 1) && ( $this -> years % 10 < 5) ) ) )
$this -> tyears = 'lata' ;
else
$this -> tyears = 'lat' ;
}
else
{
// no reaction
}
}
// show data
function display( )
{
if ( $this -> status != false )
{
{
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>' ;
}
{
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>' ;
}
else
{
echo '<font style="' . $this -> font . '"><strong>' . $this -> mess [ '4' ] . '</strong>' . '</font>' ;
}
}
else
{
// no reaction
}
}
}
przykład :
CODE : php
$t = new dtimediff( ) ;
// status, autoreverse, show seconds, show days, start, end, name event (array)
$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' ) ) ;
$t -> diff ( ) ;
$t -> display ( ) ;
online : 3 użytkowników, dziś odwiedziło : 271 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).
powered by scms © 2004 - 2025
design by sid