marando / units
一个由表示测量单位的类组成的 PHP 包
此包的规范存储库似乎已丢失,因此包已被冻结。
Requires
- php: >=5.4
Requires (Dev)
- phpunit/phpunit: ^5.0
- symfony/var-dumper: *
README
单位
Units 是一个 PHP 包,包含表示测量单位的类。
以下提供了以下单位
安装
使用 Composer
$ composer require marando/units
角度
从度和弧度创建角度
可以从度或弧度值轻松地创建 Angle
对象
Angle::deg(180); // create angle from degrees Angle::rad(3); // create angle from radians
从度、分和秒创建角度
您还可以从度、分和秒组件创建一个 Angle
对象
echo Angle::dms(180, 10, 4); // create angle from ° ' " echo Angle::dms(0, 0, 4); // create angle from just seconds
从弧分、弧秒和毫弧秒创建角度
您还可以从弧分、弧秒和毫弧秒的数量创建一个 Angle
对象
echo Angle::arcmin(15); // Create from arcmin Result: 0°15'0".0 echo Angle::arcsec(50); // Create from arcsec Result: 0°0'50".0 echo Angle::mas(310); // Create from mas Result: 0°0'0".31
获取角度的属性
可以从以下 Angle
实例获取以下属性
$angle = Angle::dms(180, 30, 0); echo $angle->deg; // degrees Output: 180.5 echo $angle->rad; // radians Output: 3.1503192998498 echo $angle->d; // degree component Output: 180 echo $angle->m; // minute component Output: 30 echo $angle->s; // second component Output: 0
规范化角度
如果您需要将 Angle
规范化到特定区间,可以这样做
echo Angle::deg(480)->norm(0, 360); // normalize to [0, +2π] Output: 120°0'0".0 echo Angle::deg>norm(0, 180); // normalize to [0, +π] Output: 160°0'0".0 echo Angle::deg->norm(-360, 360); // normalize to [-2π, +2π] Output: -360°0'0".0
角度的数学运算
Angle
对象支持加法、减法、乘法和取反等数学运算。
echo Angle::deg(90)->add(Angle::fromDeg(90)); // addition Output: 180°0'0".0 echo Angle::deg(90)->subtract(Angle::fromDeg(90)); // subtraction Output: 180°0'0".0 echo Angle::deg(90)->multiply(Angle::fromDeg(3)); // multiplication Output: 270°0'0".0 echo Angle::deg(90)->negate(); // negation Output: -90°0'0".0
角度的字符串值
默认情况下,Angle
的字符串值以 180°0'0".0
的格式表示,最多三位小数。您可以使用 round 方法指定要显示的小数位数
echo Angle::rad(3)->round(0); // round to nearest second Output: 171°53'14" echo Angle::rad(3)->round(3); // round to 3 places Output: 171°53'14".419 echo Angle::rad(3)->round(10); // round to 10 places Output: 171°53'14".4187412891
距离
创建距离
// Metric echo Distance::mm(1); // create distance from millimeters Output: 1.000 mm echo Distance::cm(1); // create distance from centimeters Output: 1.000 cm echo Distance::m(1); // create distance from meters Output: 1.000 m echo Distance::km(1); // create distance from kilometers Output: 1.000 km // Imperial echo Distance::mi(1); // create distance from miles Output: 1.000 mi // Astronomy echo Distance::au(1); // create distance from astronomical units Output: 1.000 AU echo Distance::pc(1); // create distance from parsecs Output: 1.000 pc echo Distance::ly(1); // create distance from light-years Output: 1.000 ly
Distance
对象支持加法和减法等数学运算。
$a = Distance::km(103); $b = Distance::km(7); $a->add($b); // Output: 110.000 km $a->subtract($b); // Output: 96.000 km
天文视差
可以从天文视差测量值创建距离
// Create distance from parallax of 470 milliarcsec echo $d = Distance::parallax(Angle::mas(470)) echo $d->ly; echo $d->pc; echo $d->au;
Output:
2.128 pc
6.9394973982286
2.1276595744681
438861.2898874
距离的字符串值
默认情况下,Distance
的字符串值使用三位小数表示。您可以使用 round 方法指定要显示的小数位数
echo Distance::km(2/3)->round(0); // round to nearest unit Output: 1 km echo Distance::km(2/3)->round(3); // round to 3 places Output: 0.667 km echo Distance::km(2/3)->round(10); // round to 10 places Output: 0.6666666667 km
单位之间的转换
创建 Distance
实例后,不同单位之间的转换简单
echo Distance::mi(1)->km; // get the raw kilometer value Output: 1.609344 echo Distance::mi(1)->setUnit('km'); // set units to kilometers Output: 1.609 km
setUnit()
的有效值如下: mm
、cm
、m
、km
、mi
、au
、pc
和 ly
覆盖单位定义
有时您可能希望提供距离单位定义的参数(通常用于历史应用)。例如,您可能希望创建一个 5 个天文单位的距离,但使用旧的 149597870.691
公里/天文单位的定义,而不是当前的定义。这可以通过以下方式实现
// Create distance with default and custom AU definitions $default = Distance::au(1.5); $custom = Distance::au(1.5, Distance::km(149597870.691)); // AU is the same since that's what we supplied echo $default; // Output: 1.500 AU echo $custom; // Output: 1.500 AU // Conveersions are different depending on the AU definition echo $default->km; // Output: 224396806.05 echo $custom->km; // Output: 224396806.0365
目前只能覆盖天文单位、光年和秒差距的定义参数。
压力
创建压力实例
可以像这样创建压力实例
Pressure::Pa(100); // Create pressure from Pascals Pressure::inHg(100); // Create pressure from inches of mercury Pressure::mbar(100); // Create pressure from millibars
单位之间的转换
echo Pressure::Pa(100)->inHg; echo Pressure::Pa(100)->setUnit('inHg'); echo Pressure::mbar(1000)->Pa; echo Pressure::mbar(1000)->setUnit('Pa');
Output:
0.029533372711164
0.029533372711164 inHg
100000
100000 Pa
setUnit()
的有效值如下: Pa
、inHg
和 mbar
温度
创建温度实例
可以像这样创建温度实例
Temperature::C(100); // Create temperature from Celcius Temperature::F(-32); // Create temperature from Fahrenheit Temperature::K(0); // Create temperature from Kelvins
单位之间的转换
echo Temperature::C(100)->F; echo Temperature::C(100)->setUnit('F'); echo Temperature::F(32)->C; echo Temperature::F(32)->setUnit('C'); echo Temperature::K(0)->C; echo Temperature::K(0)->setUnit('C');
Output:
212
212°F
0
0°C
-273.15
-273.15°C
setUnit()
的有效值如下: C
、F
和 K
时间
创建时间实例
创建时间实例很简单
echo Time::hms(1, 10, 15); // create time from h/m/s components Output: 1ʰ10ᵐ15ˢ echo Time::sec(30); // create time from seconds Output: 30 sec echo Time::min(4); // create time from minutes Output: 5 min echo Time::hours(2); // create time from hours Output: 2 hour echo Time::days(1); // create time from days Output: 1 day
时间实例的字符串值
默认情况下,Time
实例的字符串值使用三位小数表示。您可以使用 round 方法指定要显示的小数位数
echo Time::days(2/3)->round(0); // round to nearest unit Output: 1 day echo Time::days(2/3)->round(3); // round to 3 places Output: 0.667 days echo Time::days(2/3)->round(10); // round to 10 places Output: 0.6666666667 days
单位之间的转换
创建 Time
实例后,不同单位之间的转换简单
echo Time::days(0.54921)->min; // get the raw minute value Output: 790.8624 echo Time::days(0.54921)->setUnit('min'); // set units to minutes Output: 790.862 min
setUnit()
函数的有效值如下:sec
、min
、hours
、days
、hms
时间实例的数学运算
Time
对象支持加法和减法运算。
echo Time::sec(90)->add(Time::min(30)); // addition Output: 0ʰ31ᵐ30ˢ echo Time::sec(90)->subtract(Time::min(30)); // subtraction Output: -0ʰ28ᵐ30ˢ
获取 H/M/S 成分
可以按照以下方式获取 Time
实例的小时、分钟和秒成分
$time = Time::sec(83028); echo $time->h; // degree component Output: 21 echo $time->m; // minute component Output: 8 echo $time->s; // second component Output: 43
速度
创建速度实例
创建 Velocity
实例有两种方式
- 通过提供距离和时间成分,以及
- 使用预定义的静态构造函数
echo new Velocity(Distance::m(5), Time::sec(1)); // Method 1 Output: 5 m/s echo Velocity::ms(5); // Method 2 Output: 5 m/s
第一种方法特别适用于时间分母不均匀的情况。通过这种方式创建速度实例,例如,可以轻松地从观测数据中解析未知速度
$velocity = new Velocity(Distance::m(507), Time::sec(14.532)); echo $velocity->ms; // in m/s Output: 34.888521882742 echo $velocity->setUnit('ms'); // in m/s Output: 34.889 m/s echo $velocity->mph; // in mph Output: 78.043400775639 echo $velocity->setUnit('mph'); // in mph Output: 78.043 mph
根据第二种方法,以下预定义的单位构造函数存在
方法 | 单位 | 描述 |
---|---|---|
Velocity::kmd() |
km/d | 每天千米 |
Velocity::kmh() |
km/h | 每小时千米 |
Velocity::kms() |
km/s | 每秒千米 |
Velocity::mph() |
mph | 每小时英里 |
Velocity::ms() |
m/s | 每秒米 |
Velocity::pcy() |
pc/y | 每年秒差距 |
速度实例的字符串值
默认情况下,Velocity
实例的字符串值使用三位小数表示。您可以使用 round 方法指定要显示的小数位数
echo Velocity::mph(2/3)->round(0); // round to nearest unit Output: 1 mph echo Velocity::mph(2/3)->round(3); // round to 3 places Output: 0.667 mph echo Velocity::mph(2/3)->round(10); // round to 10 places Output: 0.6666666667 mph
单位之间的转换
创建 Velocity
实例后,不同单位之间的转换很简单
echo Velocity::mph(60)->ms; // get the raw m/s value Output: 26.8224 echo Velocity::mph(60)->setUnit('m/s'); // set units to m/s Output: 26.822 m/s
setUnit()
的有效值如下:km/d
、km/h
、km/s
、mph
、m/s
和 pc/y
速度的距离和时间成分
您可以按照以下方式获取 Velocity
实例的距离和时间成分
echo Velocity::kmh(10)->dist; // get distance component Output: 10.000 km echo Velocity::kmh(10)->time; // get time component Output: 1 hour
求解速度方程
Velocity
对象有两个方法 distance()
和 time()
,可以找到距离或时间成分,前提是提供另一个成分。这最好通过例子来说明
$v = Velocity::mph(60); // Find distance traveled in 30 min at velocity $v echo $v->dist(Time::min(30)) // Output: 30.000 mi // Find time required to travel 120 miles at velocity $v echo $v->time(Distance::mi(120)) // Output: 2 hours