当前位置: 首页 > 文档资料 > FuelPHP 中文文档 >

Date - 类別

优质
小牛编辑
124浏览
2023-12-01

Date 类别是一系列与日期协作的辅助函式。与 PHP 的 DateTime 类别相反, Fuel 的 Date 类别对 i18n 有完整的支援。你也可以使用 Date 类别的时区转换。

forge($timestamp = null, $timezone = null)

forge 方法回传一个新的 Date 物件。

静态
参数
参数预设描述
$timestamp
null
要设定给 Date 物件的时间戳记
$timezone
null
时间所设定的时区
回传Fuel\Core\Date 物件
範例
print_r(Date::forge(1294176140));

// 回传
Fuel\Core\Date Object
(
    [timestamp:protected] => 1294176140
    [timezone:protected] => UTC
)

time($timezone = null)

time 方法回传在所给时区中的目前时间做为一个 Date 物件。

静态
参数
参数预设描述
$timezone
null
时间所设定的时区
回传Fuel\Core\Date 物件
範例
print_r(Date::time());

display_timezone($timezone = null)

display_timezone 方法能让你全域地设定一个可被 format() 方法使用的时区, 以在系统时区当时产生输出另一个时区。 如果你没有传递参数,目前 display_timezone 会被回传。

静态
参数
参数预设描述
$timezone
null
要被用于在非系统时区中格式化时间戳记的显示时区。
回传混合,null、或目前显示时区如果没有参数被传递。
範例
// 设定显示时区为 'America/Lima'
Date::display_timezone('America/Lima');

create_from_string($input, $pattern_key = 'local')

create_from_string 建立一个来自具名的日期字串格式或一个模式的 Date 物件。

此函式为 i18n 用途使用 strptime()。 然而,该方法无法在 Windows 主机上使用,在这种情况下 strtotime() 被用来取代且模式会被忽略。

静态
参数
参数预设描述
$input必要要使用的日期字串
$pattern_key"local"要使用的模式键(见 config/date.php)或直接一个 strptime() 模式
回传Fuel\Core\Date 物件
範例
print_r(Date::create_from_string("01/04/2011" , "us"));

// 回传
Fuel\Core\Date Object
(
    [timestamp:protected] => 1322956800
    [timezone:protected] => UTC
)

range_to_array($start, $end, $interval = '+1 Day')

range_to_array 转换一个日期範围为一个日期物件的阵列

静态
参数
参数预设描述
$start必要开始日期,不是一个 Date 物件就是一个时间戳记
$end必要结束日期,不是一个 Date 物件就是一个时间戳记
$interval"+1 Day"要建立一个 Date 物件的时间间隔
回传阵列
範例
$start = time();
$end = $start + 604800; // 加 1 週
print_r(Date::range_to_array($start, $end, "+2 days"));

// 回传
Array
(
    [0] => Fuel\Core\Date Object
        (
            [timestamp:protected] => 1294181818
            [timezone:protected] => UTC
        )

    [1] => Fuel\Core\Date Object
        (
            [timestamp:protected] => 1294354618
            [timezone:protected] => UTC
        )

    [2] => Fuel\Core\Date Object
        (
            [timestamp:protected] => 1294527418
            [timezone:protected] => UTC
        )

    [3] => Fuel\Core\Date Object
        (
            [timestamp:protected] => 1294700218
            [timezone:protected] => UTC
        )
)

days_in_month($month, $year = null)

days_in_month 回传任何指定年、月份的天数

静态
参数
参数预设描述
$month必要要使用的月份
$year
null
要使用的年份(预设是目前年份)
回传整数
範例
echo Date::days_in_month(2);       // 28
echo Date::days_in_month(2, 2000); // 29

time_ago($timestamp, $now, $period)

time_ago 方法回传一个格式化的时间差。

静态
参数
参数预设描述
$timestamp必要UNIX 时间戳记
$now选择性要进行比较的 UNIX 时间戳记,如果没给或为空,会使用目前时间
$period选择性答案回传的时间跨度,可能的值是 'second'、'minute'、'hour'、'day'、'week'、'month'、'year' 和 'decade'。如果没有指定,会回传最大的可能值
回传字串
範例
echo Date::time_ago(strtotime("01 January 2012")); // 1 months ago(当在 February 2012 时)
echo Date::time_ago(strtotime("12 April 1964"), strtotime("01 March 2012")); // 5 decades ago
echo Date::time_ago(strtotime("12 April 1964"), strtotime("01 March 2012"), 'year'); // 48 years ago

format($pattern_key = 'local', $timezone = null)

format 方法回传一个指定模式键的格式化日期。 模式被定义在 fuel/core/config/date.php 配置档案 - 你可以藉由在 app/config 建立一个类似的档案来添加你自己的模式。

此方法为 strftime() 函式使用模式取代 date() ,以允许 i18n。

如果你没有传递一个时区,当格式化结果时,目前 Date 物件的设定时区将被使用。预设情况下, 是系统时区,除非你有藉由呼叫 set_timezone() 方法修改它。你也可以传递 true ,这将造成全域的 display_timezone 被使用。

你可以使用 display_timezone 以全域设定一个要转换到的时区,例如, 当一个使用者已登入,从一个使用者个人设定。这将能让所有日期在使用者本地时区里被显示。

静态
参数
参数预设描述
$pattern_key"local"要使用的模式键(见 config/date.php)
$timezonenull当产生格式化输出时要使用的时区
回传字串
範例
// 如果系统时间是 UTC,这将回传 "01/04/2011 21:22"
echo Date::forge(1294176140)->format("%m/%d/%Y %H:%M");

// 设定时区为 "Europe/Amsterdam"(在一月是 GMT+1),这将回传 "01/04/2011 22:22"
echo Date::forge(1294176140)->set_timezone('Europe/Amsterdam')->format("%m/%d/%Y %H:%M");

// 设定全域的显示时区为 "Europe/Amsterdam"
Date::display_timezone('Europe/Amsterdam');

// 这也将回传 "01/04/2011 22:22"
echo Date::forge(1294176140)->format("%m/%d/%Y %H:%M", true);

get_timestamp()

get_timestamp 方法回传 Date 物件的时间戳记

静态
回传整数
範例
echo Date::forge(1294176140)->get_timestamp(); // 1294176140

get_timezone()

get_timezone 方法回传 Date 物件的时区

静态
回传字串
範例
echo Date::forge(1294176140, "Europe/London")->get_timezone(); // Europe/London

get_timezone_abbr($display_timezone = false)

get_timezone_abbr 方法回传 Date 物件时区设定的时区缩写,或显示时区的时区缩写。

静态
参数
参数预设描述
$display_timezone布林如果为 true,全域的 display_timezone 将取代 Date 物件的时区设定
回传字串
範例
echo Date::forge(1294176140, "Europe/London")->get_timezone_abbr(); // 回传 "GMT"
echo Date::forge(1294176140, "Europe/Amsterdam")->get_timezone_abbr(); // 回传 "CET"

// 设定全域显示时区为 "Europe/Amsterdam"
Date::display_timezone('Europe/Amsterdam');
echo Date::forge(1294176140)->get_timezone_abbr(true); // 也回传 "CET"

set_timezone($timezone)

set_timezone 方法设定 Date 物件的时区

静态
参数
参数预设描述
$timezone必要要设定给 Date 物件的时区
回传字串
範例
echo Date::forge(1294176140)->set_timezone("America/Chicago")->get_timezone(); // America/Chicago