|
PHP入门必读(45) <?php function process_form() { global $dotw; global $month; global $day; global $year; $timestamp = mktime(0,0,0,$month,$day,$year); $next_dotw = ''; $next_timestamp = $timestamp; while ($next_dotw != $dotw) { $next_timestamp += 86400; $next_dotw = date('l',$next_timestamp); } $formatted_first = date('F d, Y',$timestamp); $formatted_next = date('F d, Y',$next_timestamp); echo "The first $dotw after $formatted_first is $formatted_next."; } ?>
首先,這段程式碼將所得到的日期以 Unix 方式計算總秒數。如果我們要更固執,更謹慎的話,我們可以加某些程式碼,以便檢查所得到的日期是否在合法的日期範圍內,不過在這裡我們沒有必要這樣做。 接下來的迴圈敘述,只要我們要尋找的某個日期的下一個星期數,不是網路用戶所輸入的要找的那一個星期數,就會一直迴圈下去。當星期數不一樣時,日期所代表的總秒數就會被增加(又是那個 86400 秒 = 24 小時 * 60 分鐘 * 60 秒)然後那個秒數所代表的星期數就會被重新計算。 一旦星期數一致了(equal),process_form() 函數就會印出一行訊息: The first Sunday after June 25, 1999 is June 27, 1999. 我們也需要那個大家都熟悉的主要敘述,將這些函數綁在一起: <?php
|