翔風 發問時間: 電腦與網際網路程式設計 · 2 0 年前

PHP~時間問題

各位大大好!因為我現在在寫一個即時競標室的程式,我想讓它競標時間一到就自動關閉並結標,並寄出一封得標信,但是我不會時間控制,像是自動倒數計時,等函式也不嘹解,我是想過用直接擷取當時時間,當擷取到的時間等於競標結束時間就停止,但是我又不知道怎麼時間相加(譬如說:結束時間=開始時間+30分鐘,我每次加出來結果都怪怪的),因為我是個php初學者,JAVA也不懂,VB只略知一二,懇請各位高手大大能夠給予一些建議或幫助,當然越簡單越好,因為我程式不強^^",感激不盡!!

4 個解答

評分
  • 匿名使用者
    2 0 年前
    最佳解答

    Unix 系統將時間計算成為 TimeStamp, 以這種方式來說. 是可以達到限時的功能的

    按您的想法而論, 您不但需要 TimeStamp 而且必須在網頁以以 JAVASCRIPT 的方式, 動態的使"按鈕"失效. 由於不知道您的資料庫結構是怎麼寫的. 有時候難以回答. 我舉個上次替某家線上錄影帶店寫的程式來說明好了.

    1. 理論基礎:

    以 mktime 函數取得結標時間的 timestamp, 您必須了解 date() 函數當中的 Y m d h i s 分別代表著 西元年, 月, 日, 小時, 分鐘, 秒.

    換句話說, 將結標時間轉成TIMESAMP計算後存入資料庫. 然後在修改資料時, 利用 date() 函數取回存入資料庫當時的TIMESTAMP(下標時間) 加以比較 資料庫當中的TIMESTAMP(結標時間). 如此一來就能夠順利的進行比對.

    為什麼要用如此複雜的方式呢? 原因在於西元曆法當中每個月有 30 天 也有 31 天. 更有 28 天的. 當你輸入資料時, 如果是 28 號. 而本月份只有 28 天. 加一天應是下個月一號. 天知道你計算出來的日期怎麼會多了好幾天 ^^!

    2. 資料庫的寫法. 為了還原當時的結標時間, 不得已使用TIMESTAMP. 可是除錯時就十分的不容易, 那麼最好存入當時的時間點. 和預計結束的時間點. 那麼當覺得計算有誤時除錯會比較容易(親身經歷打錯一個字,老是結果不對的痛苦教訓)

    3. 網頁上顯示時. 將資料庫的TIMESTAMP 轉成DATE的模式, 傳給 JAVASCRIPT. 再利用 JAVASCRIPT 進行倒數的功能, 這個部份你VB略知一二的話. 相信看懂 JAVASCRIPT 是不成問題的. 你唯一的問題可能會出現在於 PHP 讀出的東西如何 PASS 給 JAVASCRIPT. 事實上這是個老問題了, 我們假設你還不像小弟一樣的老. 您可以直接在 PHP 當中 echo "<script>.............".變數.".</script>"這種古老又簡單的方式改寫 JAVASCRIPT. 你會發現聰明的 JAVASCRIPT 是會接受您的變數的 ^^

    看了其它人的回答, 我們還是依傳統貼個原始手冊內容好了 ^^

    ----------- 引述 PHP 說明內容 MKTIME 函數篇-------------

    mktime

    (PHP 3, PHP 4 )

    mktime -- Get UNIX timestamp for a date

    Description

    int mktime ( int hour, int minute, int second, int month, int day, int year [, int is_dst])

    Warning: Note the strange order of arguments, which differs from the order of arguments in a regular UNIX mktime() call and which does not lend itself well to leaving out parameters from right to left (see below). It is a common error to mix these values up in a script.

    Returns the Unix timestamp corresponding to the arguments given. This timestamp is a long integer containing the number of seconds between the Unix Epoch (January 1 1970) and the time specified.

    Arguments may be left out in order from right to left; any arguments thus omitted will be set to the current value according to the local date and time.

    is_dst can be set to 1 if the time is during daylight savings time, 0 if it is not, or -1 (the default) if it is unknown whether the time is within daylight savings time or not. If it's unknown, PHP tries to figure it out itself. This can cause unexpected (but not incorrect) results.

    Note: is_dst was added in 3.0.10.

    mktime() is useful for doing date arithmetic and validation, as it will automatically calculate the correct value for out-of-range input. For example, each of the following lines produces the string "Jan-01-1998". Example 1. mktime() example

    echo date ("M-d-Y", mktime (0,0,0,12,32,1997));

    echo date ("M-d-Y", mktime (0,0,0,13,1,1997));

    echo date ("M-d-Y", mktime (0,0,0,1,1,1998));

    echo date ("M-d-Y", mktime (0,0,0,1,1,98));

    Year may be a two or four digit value, with values between 0-69 mapping to 2000-2069 and 70-99 to 1970-1999 (on systems where time_t is a 32bit signed integer, as most common today, the valid range for year is somewhere between 1902 and 2037).

    The last day of any given month can be expressed as the "0" day of the next month, not the -1 day. Both of the following examples will produce the string "The last day in Feb 2000 is: 29". Example 2. Last day of next month

    $lastday = mktime (0,0,0,3,0,2000);

    echo strftime ("Last day in Feb 2000 is: %d", $lastday);

    $lastday = mktime (0,0,0,4,-31,2000);

    echo strftime ("Last day in Feb 2000 is: %d", $lastday);

    Date with year, month and day equal to zero is considered illegal (otherwise it what be regarded as 30.11.1999, which would be strange behavior).

    See also date() and time().

    ----------- 引述 PHP 說明內容 DATE 函數篇-------------

    date

    (PHP 3, PHP 4 )

    date -- Format a local time/date

    Description

    string date ( string format [, int timestamp])

    Returns a string formatted according to the given format string using the given integer timestamp or the current local time if no timestamp is given.

    Note: The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer). On windows this range is limited from 01-01-1970 to 19-01-2038.

    To generate a timestamp from a string representation of the date, you may be able to use strtotime(). Additionally, some databases have functions to convert their date formats into timestamps (such as MySQL's UNIX_TIMESTAMP function).

    The following characters are recognized in the format string:

    a - "am" or "pm"

    A - "AM" or "PM"

    B - Swatch Internet time

    d - day of the month, 2 digits with leading zeros; i.e. "01" to "31"

    D - day of the week, textual, 3 letters; e.g. "Fri"

    F - month, textual, long; e.g. "January"

    g - hour, 12-hour format without leading zeros; i.e. "1" to "12"

    G - hour, 24-hour format without leading zeros; i.e. "0" to "23"

    h - hour, 12-hour format; i.e. "01" to "12"

    H - hour, 24-hour format; i.e. "00" to "23"

    i - minutes; i.e. "00" to "59"

    I (capital i) - "1" if Daylight Savings Time, "0" otherwise.

    j - day of the month without leading zeros; i.e. "1" to "31"

    l (lowercase 'L') - day of the week, textual, long; e.g. "Friday"

    L - boolean for whether it is a leap year; i.e. "0" or "1"

    m - month; i.e. "01" to "12"

    M - month, textual, 3 letters; e.g. "Jan"

    n - month without leading zeros; i.e. "1" to "12"

    O - Difference to Greenwich time in hours; e.g. "+0200"

    r - RFC 822 formatted date; e.g. "Thu, 21 Dec 2000 16:01:07 +0200" (added in PHP 4.0.4)

    s - seconds; i.e. "00" to "59"

    S - English ordinal suffix for the day of the month, 2 characters; i.e. "st", "nd", "rd" or "th"

    t - number of days in the given month; i.e. "28" to "31"

    T - Timezone setting of this machine; e.g. "EST" or "MDT"

    U - seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)

    w - day of the week, numeric, i.e. "0" (Sunday) to "6" (Saturday)

    W - ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0)

    Y - year, 4 digits; e.g. "1999"

    y - year, 2 digits; e.g. "99"

    z - day of the year; i.e. "0" to "365"

    Z - timezone offset in seconds (i.e. "-43200" to "43200"). The offset for timezones west of UTC is always negative, and for those east of UTC is always positive.

    Unrecognized characters in the format string will be printed as-is. The "Z" format will always return "0" when using gmdate().

    Example 1. date() example

    echo date ("l dS of F Y h:i:s A");

    echo "July 1, 2000 is on a " . date ("l", mktime(0,0,0,7,1,2000));

    You can prevent a recognized character in the format string from being expanded by escaping it with a preceding backslash. If the character with a backslash is already a special sequence, you may need to also escape the backslash. Example 2. Escaping characters in date()

    echo date("l \\t\h\e jS"); // prints something like 'Saturday the 8th'

    It is possible to use date() and mktime() together to find dates in the future or the past. Example 3. date() and mktime() example

    $tomorrow = mktime (0,0,0,date("m") ,date("d")+1,date("Y"));

    $lastmonth = mktime (0,0,0,date("m")-1,date("d"), date("Y"));

    $nextyear = mktime (0,0,0,date("m"), date("d"), date("Y")+1);

    Note: This can be more reliable than simply adding or subtracting the number of seconds in a day or month to a timestamp because of daylight savings time.

    Some examples of date() formatting. Note that you should escape any other characters, as any which currently have a special meaning will produce undesirable results, and other characters may be assigned meaning in future PHP versions. When escaping, be sure to use single quotes to prevent characters like \n from becoming newlines. Example 4. date() Formatting

    /* Today is March 10th, 2001, 5:16:18 pm */

    $today = date("F j, Y, g:i a"); // March 10, 2001, 5:16 pm

    $today = date("m.d.y"); // 03.10.01

    $today = date("j, n, Y"); // 10, 3, 2001

    $today = date("Ymd"); // 20010310

    $today = date('h-i-s, j-m-y, it is w Day z '); // 05-16-17, 10-03-01, 1631 1618 6 Fripm01

    $today = date('\i\t \i\s \t\h\e jS \d\a\y.'); // It is the 10th day.

    $today = date("D M j G:i:s T Y"); // Sat Mar 10 15:16:08 MST 2001

    $today = date('H:m:s \m \i\s\ \m\o\n\t\h'); // 17:03:17 m is month

    $today = date("H:i:s"); // 17:16:17

    To format dates in other languages, you should use the setlocale() and strftime() functions.

    See also getlastmod(), gmdate(), mktime(), strftime() and time().

    ------------------- 結束引用 --------------------

    依PHP手冊上這二個部份已經足夠解決你的困難點了. 程式沒有固定的寫法. 利用其它函數也可以做到. 如果您希望知道其它函數. 又不想要買書呢. 可以到 PHP.NET 去線上查即可

    參考資料: PHP 手冊
  • 6 年前

    看看他 的答案

    http://tt77.tw/

  • 6 年前

    到下面的網址看看吧

    ▶▶http://*****/

  • 匿名使用者
    7 年前

    全台唯一合法博弈網站隆重登場

    中獎彩金領取僅需5分鐘立刻到手

    快來免費試試手氣吧!

    官方網站 aa777.net

還有問題?馬上發問,尋求解答。