madbyad/mpl-date-time

一个用于高级日期和时间格式化和计算的库

v1.0.0 2024-04-15 05:00 UTC

This package is auto-updated.

Last update: 2024-09-15 06:01:24 UTC


README

mpl-date-time(MadByAd PHP 库的日期时间)是一个用于高级日期和时间系统的库。此库包括,轻松格式化和获取当前日期或/和时间,计算时间,例如20天后的日期或20天前的日期

安装

要安装此包,请打开 composer 并运行以下命令

composer require madbyad/mpl-date-time

包含

MPL DateTime 库包含 4 个类。Date 类用于高级存储和格式化日期,Time 类用于高级存储和格式化时间,DateFormat 类存储您可以使用的日期格式列表,而 DateCalculator 类用于计算日期,例如 20 天后的日期或 20 天前的日期

日期类

Date(int $time = null, bool $fromToday = false) 类用于高级存储和格式化日期。在创建新的 Date 类时,它接受两个可选参数。第一个是时间,如果未提供,则默认为 null,这将获取当前日期;如果为 0,则表示 Unix 时间戳的起始日期 1 January 1970;如果为 86400,则表示 Unix 时间戳起始日期 2 January 1970 后 1 天。第二个参数用于确定是否从 Unix 时间戳的起始日期或从今天获取时间;如果为 false,则时间 0 将获取 Unix 时间戳的起始日期;如果为 true,则时间 0 将获取当前日期,时间 86400 将获取明天,而时间 -86400 将获取昨天

示例

// This class will store the current date
new Date; 

// This class will store the beggining of the unix timestamp "1 January 1970"
new Date(0); 

// This class will store 1 day after the beggining of the unix timestamp "2 January 1970"
new Date(86400);

// This class will store the current date
new Date(0, true); 

// This class will store the date tommorow
new Date(86400, true); 

// This class will store the date yesterday
new Date(-86400, true); 

获取方法

get(string $format) 将将存储的日期数据返回为格式化后的日期字符串;格式您可以从 DateFormat 类中获取,它包含一些常量,可用于不同类型的格式

示例

$date = new Date(0);

// this will return "1/1/1970"
echo $date->get(DateFormat::D_M_YYYY);

// this will return "01/01/1970"
echo $date->get(DateFormat::DD_MM_YYYY);

// this will return "Thurday 1 January 1970 01:00"
echo $date->get(DateFormat::DAY_MONTH_YYYY__HOUR_MIN);

// this will return "1/1/1970"
echo $date->get(DateFormat::M_D_YYYY);

// this will return "01/01/1970"
echo $date->get(DateFormat::MM_DD_YYYY);

// this will return "January Thurday 1 1970 01:00"
echo $date->get(DateFormat::MONTH_DAY_YYYY__HOUR_MIN);

// this will return "1970/1/1"
echo $date->get(DateFormat::YYYY_M_D);

// this will return "1970/01/01"
echo $date->get(DateFormat::YYYY_MM_DD);

// this will return "1970 January Thurday 1 01:00"
echo $date->get(DateFormat::YYYY_MONTH_DAY__HOUR_MIN);

输出

"1/1/1970"

"01/01/1970"

"Thurday 1 January 1970 01:00"

"1/1/1970"

"01/01/1970"

"January Thurday 1 1970 01:00"

"1970/1/1"

"1970/01/01"

"1970 January Thurday 1 01:00"

获取数组方法

getArray() 方法将以关联数组的形式返回存储的日期

示例

$date = new Date(0);

var_dump($date->getArray());

输出

[
    ["day"] => "1",
    ["dayWithZero"] => "01",
    ["dayName"] => "Thursday",
    ["month"] => "1",
    ["monthWithZero"] => "01",
    ["monthName"] => "January",
    ["year"] => "1970",
    ["yearTwoDigit"] => "70",
    ["hour"] => "01",
    ["minute"] => "00",
    ["second"] => "00",
    ["unix"] => "0",
];

获取Unix方法

getUnix() 方法将返回存储的日期,形式为 Unix 时间戳

时间类

Time(int $time = 0) 类用于高级存储和格式化时间。在创建新类时,它接受一个可选参数,即时间(形式为 Unix 时间戳),默认值为 0

示例

// Create a new class that store 0 time (0 second)
new Time();

// Create a new class that store 1 day
new Time(86400);

// Create a new class that store 1 month (30 days)
new Time(86400 * 30);

设置时间

要设置时间为一定的时间量,您可以使用以下方法。它仅接受一个参数,即数量

$time = new Time();

// set the time to be 86400 second of unix timestamp (1 day)
$time->setUnix(86400);

// set the time to be 1 year
$time->setYear(1);

// set the time to be 1 month
$time->setMonth(1);

// set the time to be 1 day
$time->setDay(1);

// set the time to be 1 hour
$time->setHour(1);

// set the time to be 1 minute
$time->setMinute(1);

// set the time to be 1 second
$time->setSecond(1);

添加时间

要添加一定的时间量,您可以使用以下方法。它仅接受一个参数,即数量

$time = new Time();

// add 86400 second of unix timestamp (1 day)
$time->addUnix(86400);

// add 1 year
$time->addYear(1);

// add 1 month
$time->addMonth(1);

// add 1 day
$time->addDay(1);

// add 1 hour
$time->addHour(1);

// add 1 minute
$time->addMinute(1);

// add a second
$time->addSecond(1);

减去时间

要减去一定的时间量,您可以使用以下方法。它仅接受一个参数,即数量

$time = new Time();

// subtract 86400 second of unix timestamp (1 day)
$time->subtractUnix(86400);

// subtract 1 year
$time->subtractYear(1);

// subtract 1 month
$time->subtractMonth(1);

// subtract 1 day
$time->subtractDay(1);

// subtract 1 hour
$time->subtractHour(1);

// subtract 1 minute
$time->subtractMinute(1);

// subtract a second
$time->subtractSecond(1);

获取时间

要获取时间,您可以使用以下方法

// get the unix timestamp
$time->getUnix(86400);

// get the time year
$time->getYear(1);

// get the time month
$time->getMonth(1);

// get the time day
$time->getDay(1);

// get the time hour
$time->getHour(1);

// get the time minute
$time->getMinute(1);

// get the time second
$time->getSecond(1);

要获取关联数组形式的时间,您可以使用 get() 方法

示例

$time = new Time();

$time->addUnix(86400);
$time->addYear(1);
$time->addMonth(1);
$time->addDay(1);
$time->addHour(1);
$time->addMinute(1);
$time->addSecond(1);

var_dump($time->get());

输出

[
    ["unix"] => 33872461,
    ["year"] => 1,
    ["month"] => 1,
    ["day"] => 2,
    ["hour"] => 1,
    ["minute"] => 1,
    ["second"] => 1,
];

要获取格式化的字符串形式的时间,您可以使用 getString() 方法

示例

$time = new Time();

$time->addUnix(86400);
$time->addYear(1);
$time->addMonth(1);
$time->addDay(1);
$time->addHour(1);
$time->addMinute(1);
$time->addSecond(1);

echo $time->getString();

输出

"1 year 1 month 2 days 1 hour 1 minute 1 second"

日期计算器类

DateCalculator 类包含日期和时间计算的方法

向日期添加时间

要将时间添加到日期,请使用add(Date $date, Time $time)方法,它只接受2个参数。第一个参数是日期,第二个参数是时间

示例

$date = new Date(0);

$time = new Time();
$time->setDay(15);

$newDate = DateCalculator::add($date, $time);

echo $date->get(DateFormat::DD_MONTH_YYYY);

echo $newDate->get(DateFormat::DD_MONTH_YYYY);

输出

"01 January 1970"
"16 January 1970"

从日期减去时间

要从日期中减去时间,请使用subtract(Date $date, Time $time)方法,它也只接受2个参数。第一个参数是日期,第二个参数是时间

示例

$date = new Date(86400 * 15);

$time = new Time();
$time->setDay(5);

$newDate = DateCalculator::subtract($date, $time);

echo $date->get(DateFormat::DD_MONTH_YYYY);

echo $newDate->get(DateFormat::DD_MONTH_YYYY);

输出

"16 January 1970"
"11 January 1970"

计算时间差

要计算两个日期之间的时间差,可以使用timeGap(Date $firstDate, Date $secondDate)方法,它只接受2个参数,这两个参数都是日期

示例

$date = new Date(0);
$secondDate = new Date(86400 * 15);

$timeGap = DateCalculator::timeGap($date, $secondDate);
echo $timeGap->getString();

// also works in reverse
$timeGap = DateCalculator::timeGap($secondDate, $date);
echo $timeGap->getString();

输出

"15 days"
"15 days"

日期格式化类

DateFormat类包含一系列常量,可用于以不同的变体格式化日期