PHP Countdown

destroyaa

Mitglied
Hallo leute ..
Also da ich fast kein PHP Kan wollte ich euch fragen wie ich einen Countdown mache, also :

Er soll mir hinschreiben wielange es noch bis zu einen Bestimmten Datum ist ( Es bleibt noch 4 Tage 3 Stunden 2 Minuten 1 Sekunde bis zum EVENT ) z.B

und wen die 4 tage.... verbei sind das er mir z.B eine seite öffnet...


ich habe scho gesucht im forum aber nix gefunden...


thx für eure hilfe !
mfg
alex :)
 
Dein problem lässt sich mit PHP leider nicht wirklich lösen, da dies eine Serverseitige-Scriptsprache ist, d.h. das Script nur einmal auf dem Server ausgeführt wird und nicht auf deinem Rechner.

Dein problem kannst du aber mit JavaScript (NICHT! Java) lösen, frag mal dort im Forum nochmal.

Greez
loli
 
thx für deine schnelle antwort!

aber ein normaler counter müsste gehen oder?

also Es bleiben noch 3 tage 2 stunden 1 sekunde bis zu den event...



mit timepstapf() ( oder auch immer wie ) leider kan ich das ned
 
jop, ein normaler würde gehen, also einer, der sich nicht jede sekunde aktualisiert (und dann auch keine seite öffnet ^^).

Hab leider gerade überhaupt keine Zeit (Arbeit :( ), aber ich werde versuchen dir nachher eine kleine countDown()-Funktion zu posten.

Greez
loli
 
So, sorry für die lange Wartezeit, hatte noch einiges für meinen Chef zu machen :|

PHP:
<?php
function countdown($targetDate) {
  // Fehlermeldung, falls das Datum erreicht/berschritten wurde, hinter das Fragezeichen schreiben.
  $errorMessage = '0 Tage, 0 Monate, 0 Jahre, 0 Stunden, 0 Minuten, 0 Sekunden';
  foreach ($targetDate as $test) if (!is_integer($test)) die('Fehlerhafte Zieldatumsangabe in Funktion countdown()');
  $todayDate['day'] = date('d');
  $todayDate['month'] = date('m');
  $todayDate['year'] = date('Y');
  $todayDate['hour'] = date('H');
  $todayDate['minute'] = date('i');
  $todayDate['second'] = date('s');
  if (($todayDate['year'] > $targetDate['year']) || (($todayDate['year'] == $targetDate['year']) && ($todayDate['month'] > $targetDate['month'])) || (($todayDate['year'] == $targetDate['year']) && ($todayDate['month'] == $targetDate['month']) && ($todayDate['day'] > $targetDate['day']))) return $errorMessage;
  else {
    if ($todayDate['second'] > $targetDate['second']) {
      $countdown['seconds'] = (60 - $todayDate['second']) + $targetDate['second'];
      $sub['minute'] == TRUE;
    }
    else $countdown['seconds'] = $targetDate['second'] - $todayDate['second'];
    if ($todayDate['minute'] > $targetDate['minute']) {
      $countdown['minutes'] = (60 - $todayDate['minute']) + $targetDate['minute'];
      $sub['hour'] == TRUE;
    }
    else $countdown['minutes'] = $targetDate['minute'] - $todayDate['minute'];
    if ($sub['minute']) $countdown['minutes']--;
    if ($todayDate['hour'] > $targetDate['hour']) {
      $countdown['hours'] = (24 - $todayDate['hour']) + $targetDate['hour'];
      $sub['day'] == TRUE;
    }
    else $countdown['hours'] = $targetDate['hour'] - $todayDate['hour'];
    if ($sub['hour']) $countdown['hours']--;
    $zeiger = FALSE;
    $countdown['days'] = 0;
    if (($todayDate['year'] == $targetDate['year']) && ($todayDate['month'] == $targetDate['month']) && ($todayDate['day'] == $targetDate['day'])) $zeiger = TRUE;
    while(!$zeiger) {
      if (checkdate($todayDate['month'], $todayDate['day']+1, $todayDate['year']))
	$todayDate['day']++;
      elseif (checkdate($todayDate['month'] + 1, '1', $todayDate['year'])) {
	$todayDate['month']++;
	$todayDate['day'] = 1;
      }
      else {
	$todayDate['year']++;
	$todayDate['month'] = 1;
	$todayDate['day'] = 1;
      }
      if (($todayDate['year'] == $targetDate['year']) && ($todayDate['month'] == $targetDate['month']) && ($todayDate['day'] == $targetDate['day'])) $zeiger = TRUE;
      else $countdown['days']++;
    }
    if ($sub['day']) $countdown['days']--;
    if ($countdown['days'] < 0) return $errorMessage;
    else return 'Noch '.$countdown['days'].' Tage, '.$countdown['hours'].' Stunden, '.$countdown['minutes'].' Minuten und '.$countdown['seconds'].' Sekunden.';
    // oben: Meldung, wieviele Tage (Stunden, Minuten, Sekunden) es noch dauert, bis der Countdown abgelaufen ist, hinter das "return" schreiben.
  }   
}

##### Das Datum, bis zum dem der Countdown laufen soll #####
// Tag
$targetDate['day'] = 5;
// Monat
$targetDate['month'] = 4;
// Jahr
$targetDate['year'] = 2006;
// Stunde
$targetDate['hour'] = 7;
// Minute
$targetDate['minute'] = 45;
// Sekunde
$targetDate['second'] = 23;

// Funktionsaufruf der Funktion countdown();
echo countdown($targetDate);

## (C) by GNU
## coded by loli
?>

Dies ist die Funktion + Funktionsaufruf, an sich reicht es, wenn du das Datum, bis zu dem der Countdown läuft, auf deine Bedürfnisse anpasst, die Fehlermeldung bzw die Countdownausgabe habe ich dir aber auch einmal Kommentiert, falls du dort noch etwas ändern willst.

Greez
loli
 
dafür muss man nun wirklich kein fachinformatiker sein ;)

hab hier mal noch was ausgegraben, ich hab mal exact so ne funktion geschrieben ...

PHP:
 /**
  * countdown()
  * 
  * returns human-readable string containing the difference
  * between startime (default: now) and endtime.
  * Both parameters have to be valid UNIX-timestamps.
  * 
  * @author swEEper
  * @license GNU/GPL 
  * 
  * @param integer $endtime
  * @param integer $starttime [optional]
  * @return formatted countdown-string
  **/
 function countdown($endtime, $starttime = NULL) {
 
 	if ($starttime == NULL) 
 		$starttime = time();

	if (!is_integer($endtime) || !is_integer($starttime))
		return FALSE;
		
	$difference = $endtime - $starttime;
		
	$days = floor(($difference/86400));
   	$difference = $difference % 86400;
   	$hours = floor(($difference/3600));
   	$difference = $difference % 3600;
   	$minutes = floor(($difference/60));
   	$difference = $difference % 60;
   
   	$string = $days.' Tage und '.$hours.' Stunden und '.$minutes.' Minuten verbleibend.';
	 	
	return $string;
 }

Sollte eigentlich halbwegs verständlich sein .. ich mag übersichtlichen und simplen Code, deshalb hat mir obige Funktion ich komplett zugesagt... ist aber wohl Geschmackssache ;)

swEEper
 
xcite_swEEper hat gesagt.:
dafür muss man nun wirklich kein fachinformatiker sein ;)

hab hier mal noch was ausgegraben, ich hab mal exact so ne funktion geschrieben ...

PHP:
 /**
  * countdown()
  * 
  * returns human-readable string containing the difference
  * between startime (default: now) and endtime.
  * Both parameters have to be valid UNIX-timestamps.
  * 
  * @author swEEper
  * @license GNU/GPL 
  * 
  * @param integer $endtime
  * @param integer $starttime [optional]
  * @return formatted countdown-string
  **/
 function countdown($endtime, $starttime = NULL) {
 
 	if ($starttime == NULL) 
 		$starttime = time();

	if (!is_integer($endtime) || !is_integer($starttime))
		return FALSE;
		
	$difference = $endtime - $starttime;
		
	$days = floor(($difference/86400));
   	$difference = $difference % 86400;
   	$hours = floor(($difference/3600));
   	$difference = $difference % 3600;
   	$minutes = floor(($difference/60));
   	$difference = $difference % 60;
   
   	$string = $days.' Tage und '.$hours.' Stunden und '.$minutes.' Minuten verbleibend.';
	 	
	return $string;
 }

Sollte eigentlich halbwegs verständlich sein .. ich mag übersichtlichen und simplen Code, deshalb hat mir obige Funktion ich komplett zugesagt... ist aber wohl Geschmackssache ;)

swEEper


:rolleyes: das war auf den Programmierstill bezogen nicht auf diese "hochkomplexe" Funktion ;-)

Nette vorgehensweise swEEper :)
 
Zurück