<?php /* Description : [[calculer un délai entre deux dates]] p.ex. pour afficher une information x avant la date y puis une information z Auteur : Fred Radeff Url : http://www.akademia.ch keyword: maketime, strtotime, time, date, délai */ #petite fonction pour transformer les seconde en h:m:s function sec2hms ($sec, $padHours = false) //source: http://www.laughing-buddha.net/jon/php/sec2hms/ { // holds formatted string $hms = ""; // there are 3600 seconds in an hour, so if we // divide total seconds by 3600 and throw away // the remainder, we've got the number of hours $hours = intval(intval($sec) / 3600); // add to $hms, with a leading 0 if asked for $hms .= ($padHours) ? str_pad($hours, 2, "0", STR_PAD_LEFT). ':' : $hours. ':'; // dividing the total seconds by 60 will give us // the number of minutes, but we're interested in // minutes past the hour: to get that, we need to // divide by 60 again and keep the remainder $minutes = intval(($sec / 60) % 60); // then add to $hms (with a leading 0 if needed) $hms .= str_pad($minutes, 2, "0", STR_PAD_LEFT). ':'; // seconds are simple - just divide the total // seconds by 60 and keep the remainder $seconds = intval($sec % 60); // add to $hms, again with a leading 0 if needed $hms .= str_pad($seconds, 2, "0", STR_PAD_LEFT); // done! return $hms; } $aujourdhui=date("U", mktime()); $delai = mktime(0,0,0,3,1,2009); //March 1, 2009 $tempsrestant=$delai-$aujourdhui; echo "<pre>"; echo "Délai: " .date("d-m-Y H:i", $delai) ."\n"; echo "Maintenant: " .date("d-m-Y H:i", $aujourdhui) ."\n"; echo " Différence: " .$tempsrestant ." secondes\n\n"; if($tempsrestant<0) { echo "Délai dépassé"; } else { echo "Délai courant, il vous reste " .sec2hms($tempsrestant) ." h:m:s"; } echo "</pre>"; ?>