Loading... PHP 手冊 函數參考 日期與時間相關擴展 日期/時間 Date/Time 函數 == date (PHP 4, PHP 5, PHP 7) date — 格式化一個本地時間/日期 說明 date ( string $format [, int $timestamp ] ) : string 返回將整數 timestamp 按照給定的格式字串而產生的字符串。如果沒有給出時間戳則使用本地當前時間。換句話說,timestamp 是可選的,默認值為 time()。 Tip 自 PHP 5.1.1 起有幾個有用的常量可用作標準的日期/時間格式來指定 format 參數。 Tip 自 PHP 5.1 起在 $_SERVER['REQUEST_TIME'] 中保存了發起該請求時刻的時間戳。 Note: 有效的時間戳典型范圍是格林威治時間 1901 年 12 月 13 日 20:45:54 到 2038 年 1 月 19 日 03:14:07。(此范圍符合 32 位有符號整數的最小值和最大值)。不過在 PHP 5.1 之前此范圍在某些系統(如 Windows)中限制為從 1970 年 1 月 1 日到 2038 年 1 月 19 日。 Note: 要將字符串表達的時間轉換成時間戳,應該使用 strtotime()。此外一些數據庫有一些函數將其時間格式轉換成時間戳(例如 MySQL 的 ? UNIX_TIMESTAMP 函數)。 格式字串可以識別以下 format 參數的字符串 format 字符 說明 返回值例子 日 --- --- d 月份中的第幾天,有前導零的 2 位數字 01 到 31 D 星期中的第幾天,文本表示,3 個字母 Mon 到 Sun j 月份中的第幾天,沒有前導零 1 到 31 l(“L”的小寫字母) 星期幾,完整的文本格式 Sunday 到 Saturday N ISO-8601 格式數字表示的星期中的第幾天(PHP 5.1.0 新加) 1(表示星期一)到 7(表示星期天) S 每月天數后面的英文后綴,2 個字符 st,nd,rd 或者 th??梢院?j 一起用 w 星期中的第幾天,數字表示 0(表示星期天)到 6(表示星期六) z 年份中的第幾天 0 到 365 星期 --- --- W ISO-8601 格式年份中的第幾周,每周從星期一開始(PHP 4.1.0 新加的) 例如:42(當年的第 42 周) 月 --- --- F 月份,完整的文本格式,例如 January 或者 March January 到 December m 數字表示的月份,有前導零 01 到 12 M 三個字母縮寫表示的月份 Jan 到 Dec n 數字表示的月份,沒有前導零 1 到 12 t 指定的月份有幾天 28 到 31 年 --- --- L 是否為閏年 如果是閏年為 1,否則為 0 o ISO-8601 格式年份數字。這和 Y 的值相同,只除了如果 ISO 的星期數(W)屬于前一年或下一年,則用那一年。(PHP 5.1.0 新加) Examples: 1999 or 2003 Y 4 位數字完整表示的年份 例如:1999 或 2003 y 2 位數字表示的年份 例如:99 或 03 時間 --- --- a 小寫的上午和下午值 am 或 pm A 大寫的上午和下午值 AM 或 PM B Swatch Internet 標準時 000 到 999 g 小時,12 小時格式,沒有前導零 1 到 12 G 小時,24 小時格式,沒有前導零 0 到 23 h 小時,12 小時格式,有前導零 01 到 12 H 小時,24 小時格式,有前導零 00 到 23 i 有前導零的分鐘數 00 到 59> s 秒數,有前導零 00 到 59> u 毫秒 (PHP 5.2.2 新加)。需要注意的是 date() 函數總是返回 000000 因為它只接受 integer 參數, 而 DateTime::format() 才支持毫秒。 示例: 654321 時區 --- --- e 時區標識(PHP 5.1.0 新加) 例如:UTC,GMT,Atlantic/Azores I 是否為夏令時 如果是夏令時為 1,否則為 0 O 與格林威治時間相差的小時數 例如:+0200 P 與格林威治時間(GMT)的差別,小時和分鐘之間有冒號分隔(PHP 5.1.3 新加) 例如:+02:00 T 本機所在的時區 例如:EST,MDT(【譯者注】在 Windows 下為完整文本格式,例如“Eastern Standard Time”,中文版會顯示“中國標準時間”)。 Z 時差偏移量的秒數。UTC 西邊的時區偏移量總是負的,UTC 東邊的時區偏移量總是正的。 -43200 到 43200 完整的日期/時間 --- --- c ISO 8601 格式的日期(PHP 5 新加) 2004-02-12T15:19:21+00:00 r RFC 822 格式的日期 例如:Thu, 21 Dec 2000 16:01:07 +0200 U 從 Unix 紀元(January 1 1970 00:00:00 GMT)開始至今的秒數 參見 time() 格式字串中不能被識別的字符將原樣顯示。Z 格式在使用 gmdate() 時總是返回 0。 Example #1 date() 例子 <?php // 設定要用的默認時區。自 PHP 5.1 可用 date_default_timezone_set('UTC'); // 輸出類似:Monday echo date("l"); // 輸出類似:Monday 15th of August 2005 03:12:46 PM echo date('l dS \of F Y h:i:s A'); // 輸出:July 1, 2000 is on a Saturday echo "July 1, 2000 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2000)); /* 在格式參數中使用常量 */ // 輸出類似:Wed, 25 Sep 2013 15:28:57 -0700 echo date(DATE_RFC2822); // 輸出類似:2000-07-01T00:00:00+00:00 echo date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000)); ?> 在格式字串中的字符前加上反斜線來轉義可以避免它被按照上表解釋。如果加上反斜線后的字符本身就是一個特殊序列,那還要轉義反斜線。 Example #2 在 date() 中轉義字符 <?php // prints something like: Wednesday the 15th echo date("l \\t\h\e jS"); ?> 可以把 date() 和 mktime() 函數結合使用來得到未來或過去的日期。 Example #3 date() 和 mktime() 例子 <?php $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: 由于夏令時的緣故,這種方法比簡單地在時間戳上加減一天或者一個月的秒數更可靠。 一些使用 date() 格式化日期的例子。注意要轉義所有其它的字符,因為目前有特殊含義的字符會產生不需要的結果,而其余字符在 PHP 將來的版本中可能會被用上。當轉義時,注意用單引號以避免類似 \n 的字符變成了換行符。 Example #4 date() 格式舉例 <?php // 假定今天是: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 $today = date("Y-m-d H:i:s"); // 2001-03-10 17:16:18 (MySQL DATETIME 格式) ?> 要格式化其它語種的日期,應該用 setlocale() 和 strftime() 函數來代替 date()。 參見 getlastmod(),gmdate(),mktime(),strftime() 和 time()。 參數 format 輸出的日期 string 格式。 參見下文中的 格式化選項。 同時,還可以使用 預定義日期常量 ,例如:常量 DATE_RSS 表示格式化字符串 'D, d M Y H:i:s'。 format 參數字符串中可識別下列字符 format 字符 描述 返回值示例 天 --- --- d 一個月中的第幾天,有前導 0 的 2 位數字 從 01 到 31 D 3 個字符表示的星期幾 從 Mon 到 Sun j 一個月中的第幾天,無前導 0 從 1 到 31 l (lowercase 'L') 星期幾,英文全稱 從 Sunday 到 Saturday N ISO-8601 規定的數字表示的星期幾(PHP 5.1.0 新加 ) 從 1 (表示星期一)到 7 (表示星期日) S 一個月中的第幾天,帶有 2 個字符表示的英語序數詞。 st, nd, rd 或者 th。 可以和 j 聯合使用。 w 數字表示的星期幾 從 0 (星期日) 到 6 (星期六) z 一年中的第幾天,從 0 開始計數 從 0 到 365 周 --- --- W ISO-8601 規范的一年中的第幾周,周一視為一周開始。(PHP 4.1.0 新加) 示例: 42 (本年第42周) 月 --- --- F 月份英文全拼,例如:January 或 March 從 January 到 December m 帶有 0 前導的數字表示的月份 從 01 到 12 M 3 個字符表示的月份的英文簡拼 從 Jan 到 Dec n 月份的數字表示,無前導 0 1 through 12 t 給定月份中包含多少天 從 28 到 31 年 --- --- L 是否為閏年 如果是閏年,則返回 1,反之返回 0。 o ISO-8601 規范的年份,同 Y 格式。有一種情況除外:當 ISO 的周數(W)屬于前一年或者后一年時,會返回前一年或者后一年的年份數字表達。 屬于前一年或者后一年時,會返回前一年或者后一年的年份數字表達。 (PHP 5.1.0 新加) 示例:1999 或 2003 Y 4 位數字的年份 示例:1999 或 2003 y 2 位數字的年份 示例: 99 或 03 時間 --- --- a 上午還是下午,2 位小寫字符 am 或 pm A 上午還是下午,2 位大寫字符 AM 或 PM B 斯沃琪因特網時間 從 000 到 999 g 小時,12時制,無前導 0 從 1 到 12 G 小時,24時制,無前導 0 從 0 到 23 h 小時,12時制,有前導 0 的 2 位數字 從 01 到 12 H 小時,24時制,有前導 0 的 2 位數字 00 through 23 i 分鐘,有前導 0 的 2 位數字 從 00 到 59 s 秒,有前導 0 的 2 位數字 從 00 到 59 u 毫秒 (PHP 5.2.2 新加) 示例: 654321 時區 --- --- e 時區標識(PHP 5.1.0 新加) 示例: UTC, GMT, Atlantic/Azores I (大寫字母 i) 是否夏令時 如果是夏令時則返回 1,反之返回 0。 O 和格林威治時間(GMT)的時差,以小時為單位 示例: +0200 P 和格林威治時間(GMT)的時差,包括小時和分鐘,小時和分鐘之間使用冒號(:)分隔(PHP 5.1.3 新加) 示例: +02:00 T 時區縮寫 示例:EST, MDT ... Z 以秒為單位的時區偏移量。UTC 以西的時區返回負數,UTC 以東的時區返回正數。 從 -43200 到 50400 完整的日期/時間 --- --- c ISO 8601 日期及時間(PHP 5 新加) 2004-02-12T15:19:21+00:00 r ? RFC 2822 格式的日期和時間 示例:Thu, 21 Dec 2000 16:01:07 +0200 U 自 1970 年 1 月 1 日 0 時 0 分 0 秒(GMT 時間)以來的時間,以秒為單位 參見time() 格式化字符串中的不可識別字符將原樣輸出。 當使用 gmdate() 函數時, Z 格式永遠返回 0。 Note: 由于本函數僅接受 integer 類型的時間戳參數,所以 u 格式僅在使用 date_format() 函數并且使用 date_create() 函數創建時間戳時才是有用的。 timestamp 可選的 timestamp 參數是一個 integer 的 Unix 時間戳,如未指定,參數值默認為當前本地時間。也就是說,其值默認為 time() 的返回值。 返回值 返回格式化后的日期時間的字符串表達。 如果 timestamp 參數不是一個有效數值,則返回 FALSE 并引發 E_WARNING 級別的錯誤。 錯誤/異常 在每 次調用日期/時間函數時,如果時區無效則會引發 E_NOTICE 錯誤,如果使用系統設定值或 TZ 環境變量,則會引發 E_STRICT 或 E_WARNING 消息。參見 date_default_timezone_set()。 更新日志 版本 說明 5.1.0 時間戳的有效取值范圍為 GMT 時間的 1901 年 12 月 13 日至 GMT 時間的 2038 年 1 月 19 日。 (32 位有符號整數的取值范圍)。 但是,在 PHP 5.1.0 之前的版本,在某些系統(例如 Windows)上有效取值范圍為 1970 年 1 月 1 日至 2038 年 1 月 19 日。 5.1.0 現在發布 E_STRICT 和 E_NOTICE 時區錯誤。 5.1.1 format 參數標準的可用日期/時間格式常量見: 常量 范例 Example #5 date() 函數示例 <?php // 設置默認時區。PHP 5.1 之后版本可用 date_default_timezone_set('UTC'); // 輸出類似: Monday echo date("l"); // 輸出類似:Monday 8th of August 2005 03:12:46 PM echo date('l jS \of F Y h:i:s A'); // 輸出:July 1, 2000 is on a Saturday echo "July 1, 2000 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2000)); /* 使用格式常量 */ // 輸出類似: Mon, 15 Aug 2005 15:12:46 UTC echo date(DATE_RFC822); // 輸出類似:2000-07-01T00:00:00+00:00 echo date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000)); ?> 可以使用反斜線進行轉義來阻止函數解析格式字符串中的可識別字符。 如果反斜線和要轉義的字符連在一起依然是一個有效的字符序列,那么需要對 反斜線再次進行轉義。 Example #6 對 date() 函數中的格式字符串進行轉義 <?php // 輸出類似: Wednesday the 15th echo date('l \t\h\e jS'); ?> 可以聯合使用 date() 和 mktime() 函數 來構造之前或者之后的日期時間。 Example #7 date() 和 mktime() 聯合使用示例 <?php $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: 由于存在夏令時時間, 所以此方案相對于直接在時間戳上加/減秒數 要更加可靠。 date() 函數格式化的一些示例。 需要注意的是,即使是對于當前來說并不具有特殊含義的字符, 也要像對待具有特殊含義的字符那樣進行轉義,以避免函數返回非預期的值。 因為可能在將來的 PHP 版本中,這些字符會被賦予特殊的含義。 進行轉義的時候,請確保使用單引號,以避免 \n 被解釋為換行符號。 Example #8 date() 函數格式化 <?php // 假設今天是 2001 年 3 月 10 日下午 5 點 16 分 18 秒, // 并且位于山區標準時間(MST)時區 $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'); // 05-16-18, 10-03-01, 1631 1618 6 Satpm01 $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 17:16:18 MST 2001 $today = date('H:m:s \m \i\s\ \m\o\n\t\h'); // 17:03:18 m is month $today = date("H:i:s"); // 17:16:18 ?> 如果需要將日期時間格式化為其他語言,你應該使用 setlocale() 和 strftime() 函數 來替代 date() 函數。 注釋 Note: 使用 strtotime() 函數將一個字符串表達的日期時間轉換為時間戳。 另外,一些數據庫產品也提供了將日期時間格式轉換為時間戳的函數。 (例如 MySQL 中的 ? UNIX_TIMESTAMP 函數)。 Tip 從 PHP 5.1 版本開始,請求的開始時間可以從變量 $_SERVER['REQUEST_TIME'] 中獲取。 參見 gmdate() - 格式化一個 GMT/UTC 日期/時間 idate() - 將本地時間日期格式化為整數 getdate() - 取得日期/時間信息 getlastmod() - 獲取頁面最后修改的時間 mktime() - 取得一個日期的 Unix 時間戳 strftime() - 根據區域設置格式化本地時間/日期 time() - 返回當前的 Unix 時間戳 strtotime() - 將任何字符串的日期時間描述解析為 Unix 時間戳 預定義的日期時間常量 add a note add a note User Contributed Notes 18 notes up down 106 Jimmy ?8 years ago Things to be aware of when using week numbers with years. <?php echo date("YW", strtotime("2011-01-07")); // gives 201101 echo date("YW", strtotime("2011-12-31")); // gives 201152 echo date("YW", strtotime("2011-01-01")); // gives 201152 too ?> BUT <?php echo date("oW", strtotime("2011-01-07")); // gives 201101 echo date("oW", strtotime("2011-12-31")); // gives 201152 echo date("oW", strtotime("2011-01-01")); // gives 201052 (Year is different than previous example) ?> Reason: Y is year from the date o is ISO-8601 year number W is ISO-8601 week number of year Conclusion: if using 'W' for the week number use 'o' for the year. up down 18 adityabhai at gmail dot com ?6 years ago For Microseconds, we can get by following: echo date('Ymd His'.substr((string)microtime(), 1, 8).' e'); Thought, it might be useful to someone ! up down 7 david dot thomas at elliott-thomas dot com dot au ?3 years ago Prior to PHP 5.6.23, Relative Formats for the start of the week aligned with PHP's (0=Sunday,6=Saturday). Since 5.6.23, Relative Formats for the start of the week align with ISO-8601 (1=Monday,7=Sunday). (http://php.net/manual/en/datetime.formats.relative.php) This can produce different, and seemingly incorrect, results depending on your PHP version and your choice of 'w' or 'N' for the Numeric representation of the day of the week: <?php echo "Today is Sun 2 Oct 2016, day ",date('w',strtotime('2016-10-02'))," of this week. "; echo "Day ",date('w',strtotime('2016-10-02 Monday next week'))," of next week is ",date('d M Y',strtotime('2016-10-02 Monday next week')),"<br />"; echo "Today is Sun 2 Oct 2016, day ",date('N',strtotime('2016-10-02'))," of this week. "; echo "Day ",date('w',strtotime('2016-10-02 Monday next week'))," of next week is ",date('d M Y',strtotime('2016-10-02 Monday next week')); ?> Prior to PHP 5.6.23, this results in: Today is Sun 2 Oct 2016, day 0 of this week. Day 1 of next week is 10 Oct 2016 Today is Sun 2 Oct 2016, day 7 of this week. Day 1 of next week is 10 Oct 2016 Since PHP 5.6.23, this results in: Today is Sun 2 Oct 2016, day 0 of this week. Day 1 of next week is 03 Oct 2016 Today is Sun 2 Oct 2016, day 7 of this week. Day 1 of next week is 03 Oct 2016 up down 9 Charlie ?4 years ago For HTML5 datetime-local HTML input controls (http://www.w3.org/TR/html-markup/input.datetime-local.html) use format example: 1996-12-19T16:39:57 To generate this, escape the 'T', as shown below: <?php date('Y-m-d\TH:i:s'); ?> up down 15 FiraSEO ?7 years ago this how you make an HTML5 <time> tag correctly <?php echo '<time datetime="'.date('c').'">'.date('Y - m - d').'</time>'; ?> in the "datetime" attribute you should put a machine-readable value which represent time , the best value is a full time/date with ISO 8601 ( date('c') ) ,,, the attr will be hidden from users and it doesn't really matter what you put as a shown value to the user,, any date/time format is okay ! This is very good for SEO especially search engines like Google . up down 7 Anonymous ?4 years ago If timestamp is a string, date converts it to an integer in a possibly unexpected way: <?php echo (int)'0x10'; //0 echo intval('0x10'); //0 echo date('s', '0x10'); //gives 16 //however, no octal conversion: echo date('s', '010'); //gives 10 ?> (PHP 5.6.16) up down 10 Anonymous ?6 years ago It's common for us to overthink the complexity of date/time calculations and underthink the power and flexibility of PHP's built-in functions. Consider http://php.net/manual/en/function.date.php#108613 <?php function get_time_string($seconds) { return date('H:i:s', strtotime("2000-01-01 + $seconds SECONDS")); } up down 10 matthew dot hotchen at worldfirst dot com ?6 years ago FYI: there's a list of constants with predefined formats on the DateTime object, for example instead of outputting ISO 8601 dates with: <?php echo date('c'); ?> or <?php echo date('Y-m-d\TH:i:sO'); ?> You can use <?php echo date(DateTime::ISO8601); ?> instead, which is much easier to read. up down 10 ivijan dot stefan at gmail dot com ?5 years ago If you have a problem with the different time zone, this is the solution for that. <?php // first line of PHP $defaultTimeZone='UTC'; if(date_default_timezone_get()!=$defaultTimeZone)) date_default_timezone_set($defaultTimeZone); // somewhere in the code function _date($format="r", $timestamp=false, $timezone=false) { $userTimezone = new DateTimeZone(!empty($timezone) ? $timezone : 'GMT'); $gmtTimezone = new DateTimeZone('GMT'); $myDateTime = new DateTime(($timestamp!=false?date("r",(int)$timestamp):date("r")), $gmtTimezone); $offset = $userTimezone->getOffset($myDateTime); return date($format, ($timestamp!=false?(int)$timestamp:$myDateTime->format('U')) + $offset); } /* Example */ echo 'System Date/Time: '.date("Y-m-d | h:i:sa").'<br>'; echo 'New York Date/Time: '._date("Y-m-d | h:i:sa", false, 'America/New_York').'<br>'; echo 'Belgrade Date/Time: '._date("Y-m-d | h:i:sa", false, 'Europe/Belgrade').'<br>'; echo 'Belgrade Date/Time: '._date("Y-m-d | h:i:sa", 514640700, 'Europe/Belgrade').'<br>'; ?> This is the best and fastest solution for this problem. Working almost identical to date() function only as a supplement has the time zone option. up down 4 rc at macshot dot de ?3 years ago At least in PHP 5.5.38 date('j.n.Y', 2222222222) gives a result of 2.6.2040. So date is not longer limited to the minimum and maximum values for a 32-bit signed integer as timestamp. up down 2 mirco dot babin at gmail dot com ?1 year ago One important thing you should remember is that the timestamp value returned by time() is time-zone agnostic and gets the number of seconds since 1 January 1970 at 00:00:00 UTC. This means that at a particular point in time, this function will return the same value in the US, Europe, India, Japan, ... date() will format a time-zone agnostic timestamp according to the default timezone set with date_default_timezone_set(...). Local time. If you want to output as UTC time use: <?php function dateUTC($format, $timestamp = null) { if ($timestamp === null) $timestamp = time(); $tz = date_default_timezone_get(); date_default_timezone_set('UTC'); $result = date($format, $timestamp); date_default_timezone_set($tz); return $result; } /> up down 4 SpikeDaCruz ?14 years ago The following function will return the date (on the Gregorian calendar) for Orthodox Easter (Pascha). Note that incorrect results will be returned for years less than 1601 or greater than 2399. This is because the Julian calendar (from which the Easter date is calculated) deviates from the Gregorian by one day for each century-year that is NOT a leap-year, i.e. the century is divisible by 4 but not by 10. (In the old Julian reckoning, EVERY 4th year was a leap-year.) This algorithm was first proposed by the mathematician/physicist Gauss. Its complexity derives from the fact that the calculation is based on a combination of solar and lunar calendars. <?php function getOrthodoxEaster($date){ /* Takes any Gregorian date and returns the Gregorian date of Orthodox Easter for that year. */ $year = date("Y", $date); $r1 = $year % 19; $r2 = $year % 4; $r3 = $year % 7; $ra = 19 * $r1 + 16; $r4 = $ra % 30; $rb = 2 * $r2 + 4 * $r3 + 6 * $r4; $r5 = $rb % 7; $rc = $r4 + $r5; //Orthodox Easter for this year will fall $rc days after April 3 return strtotime("3 April $year + $rc days"); } ?> up down 0 Bas Vijfwinkel ?8 years ago Note that some formatting options are different from MySQL. For example using a 24 hour notation without leading zeros is the option '%G' in PHP but '%k' in MySQL. When using dynamically generated date formatting string, be careful to generate the correct options for either PHP or MySQL. up down -1 bruslbn at gmail dot com ?2 years ago In order to define leap year you must considre not only that year can be divide by 4! The correct alghoritm is: if (year is not divisible by 4) then (it is a common year) else if (year is not divisible by 100) then (it is a leap year) else if (year is not divisible by 400) then (it is a common year) else (it is a leap year) So the code should look like this: if($year%4 == 0 && $year%100 != 0) { $leapYear = 1; } elseif($year%400 == 0) { $leapYear = 1; } else { $leapYear = 0; } up down 0 ghotinet ?9 years ago Most spreadsheet programs have a rather nice little built-in function called NETWORKDAYS to calculate the number of business days (i.e. Monday-Friday, excluding holidays) between any two given dates. I couldn't find a simple way to do that in PHP, so I threw this together. It replicates the functionality of OpenOffice's NETWORKDAYS function - you give it a start date, an end date, and an array of any holidays you want skipped, and it'll tell you the number of business days (inclusive of the start and end days!) between them. I've tested it pretty strenuously but date arithmetic is complicated and there's always the possibility I missed something, so please feel free to check my math. The function could certainly be made much more powerful, to allow you to set different days to be ignored (e.g. "skip all Fridays and Saturdays but include Sundays") or to set up dates that should always be skipped (e.g. "skip July 4th in any year, skip the first Monday in September in any year"). But that's a project for another time. <?php function networkdays($s, $e, $holidays = array()) { // If the start and end dates are given in the wrong order, flip them. if ($s > $e) return networkdays($e, $s, $holidays); // Find the ISO-8601 day of the week for the two dates. $sd = date("N", $s); $ed = date("N", $e); // Find the number of weeks between the dates. $w = floor(($e - $s)/(86400*7)); # Divide the difference in the two times by seven days to get the number of weeks. if ($ed >= $sd) { $w--; } # If the end date falls on the same day of the week or a later day of the week than the start date, subtract a week. // Calculate net working days. $nwd = max(6 - $sd, 0); # If the start day is Saturday or Sunday, add zero, otherewise add six minus the weekday number. $nwd += min($ed, 5); # If the end day is Saturday or Sunday, add five, otherwise add the weekday number. $nwd += $w * 5; # Add five days for each week in between. // Iterate through the array of holidays. For each holiday between the start and end dates that isn't a Saturday or a Sunday, remove one day. foreach ($holidays as $h) { $h = strtotime($h); if ($h > $s && $h < $e && date("N", $h) < 6) $nwd--; } return $nwd; } $start = strtotime("1 January 2010"); $end = strtotime("13 December 2010"); // Add as many holidays as desired. $holidays = array(); $holidays[] = "4 July 2010"; // Falls on a Sunday; doesn't affect count $holidays[] = "6 September 2010"; // Falls on a Monday; reduces count by one echo networkdays($start, $end, $holidays); // Returns 246 ?> Or, if you just want to know how many work days there are in any given year, here's a quick function for that one: <?php function workdaysinyear($y) { $j1 = mktime(0,0,0,1,1,$y); if (date("L", $j1)) { if (date("N", $j1) == 6) return 260; elseif (date("N", $j1) == 5 or date("N", $j1) == 7) return 261; else return 262; } else { if (date("N", $j1) == 6 or date("N", $j1) == 7) return 260; else return 261; } } ?> up down -6 arth dot inbox at gmail dot com ?1 year ago Looks like date('u') is not microseconds, but is positive difference from rest part. php > echo (DateTime::createFromFormat('U.u', '-128649659.999998'))->format('Y-m-d H:i:s.u U.u'); 1965-12-03 23:59:01.999998 -128649659.999998 `U.u` parsed and formatted same, but means not 1965-12-03 23:59:00.000002. Other words correct timestamp for example above is (-128649659 + 0.999998). Less confusing format for it is: php > echo DateTime::createFromFormat('U\+0.u', '-128649660+0.000002')->format('Y-m-d H:i:s.u'); 1965-12-03 23:59:00.000002 Is that bug or feature? up down -10 bruslbn at gmail dot com ?2 years ago In order to define leap year you must considre not only that year can be divide by 4! The correct alghoritm is: if (year is not divisible by 4) then (it is a common year) else if (year is not divisible by 100) then (it is a leap year) else if (year is not divisible by 400) then (it is a common year) else (it is a leap year) So the code should look like this: if($year%4 == 0 && $year%100 != 0) { $leapYear = 1; } elseif($year%400 == 0) { $leapYear = 1; } else { $leapYear = 0; } up down -12 bruslbn at gmail dot com ?2 years ago In order to define leap year you must considre not only that year can be divide by 4! The correct alghoritm is: if (year is not divisible by 4) then (it is a common year) else if (year is not divisible by 100) then (it is a leap year) else if (year is not divisible by 400) then (it is a common year) else (it is a leap year) So the code should look like this: if($year%4 == 0 && $year%100 != 0) { $leapYear = 1; } elseif($year%400 == 0) { $leapYear = 1; } else { $leapYear = 0; } add a note add a note PHP: date - Manual [https://www.php.net/manual/zh/function.date.php][1] [1]: https://www.php.net/manual/zh/function.date.php 最后修改: ? 允許規范轉載 打賞 贊賞作者 支付寶微信 贊 如果覺得我的文章對你有用,請隨意贊賞