0

我使用这个倒计时脚本,每天倒计时到下午 5 点。如何在没有秒的情况下打印时间?

<?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;
            }
        ?>
4

1 回答 1

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;
            }

?>
于 2012-11-26T12:29:46.387 回答