2012-11-26 140 views
0

我使用這個倒計時腳本,每天倒數到下午5點。如何在沒有秒的情況下打印時間?PHP沒有秒倒計時

<?php 
      function formatTime($unixtime) { 
       return date("H:i", $unixtime); 
      } 

      function formatSeconds($seconds) { 
       $time = str_pad(intval(intval($seconds/3600)),2,"0",STR_PAD_LEFT).":" 
       . str_pad(intval(($seconds/60) % 60),2,"0",STR_PAD_LEFT).":" 
       . str_pad(intval($seconds % 60),2,"0",STR_PAD_LEFT) ; 
       return $time; 
      } 

      date_default_timezone_set('Europe/Amsterdam'); 
      $hour_in_english = "5pm"; 
      $passed_message = "Bestel nu en je bestelling vertekt morgen!"; 
      $future_message = "Bestel binnen "; 
      $future_message_2 = " en je bestelling vertrekt vandaag!"; 
      $time_now = strtotime("now"); 
      $time_hour = strtotime("today {$hour_in_english} "); 
      $difference_in_seconds = $time_hour - $time_now; 
      if ($difference_in_seconds < 0) { 
       print $passed_message; 
      } else { 
       print $future_message . formatSeconds($difference_in_seconds) . $future_message_2; 
      } 
     ?> 
+0

只是想知道爲什麼你使用PHP,而不是JavaScript? – Ben

+0

不幸的是,我無法在該頁面上使用JavaScript。所以我需要使用PHP – Aduro

回答

1

在任何情況下,你就必須從formatSeconds功能刪除代碼。這裏是更新後的代碼,只需打印沒有秒的時間。

<?php 
     function formatTime($unixtime) { 
       return date("H:i", $unixtime); 
      } 

      function formatSeconds($seconds) { 
       $time = str_pad(intval(intval($seconds/3600)),2,"0",STR_PAD_LEFT).":" 
       . str_pad(intval(($seconds/60) % 60),2,"0",STR_PAD_LEFT); 
       return $time; 
      } 

      date_default_timezone_set('Europe/Amsterdam'); 
      $hour_in_english = "5pm"; 
      $passed_message = "Bestel nu en je bestelling vertekt morgen!"; 
      $future_message = "Bestel binnen "; 
      $future_message_2 = " en je bestelling vertrekt vandaag!"; 
      $time_now = strtotime("now"); 
      $time_hour = strtotime("today {$hour_in_english} "); 
      $difference_in_seconds = $time_hour - $time_now; 
      if ($difference_in_seconds < 0) { 
       print $passed_message; 
      } else { 
       print $future_message . formatSeconds($difference_in_seconds) . $future_message_2; 
      } 

?>