[TOC]
## 日期
> 注意“加減時”的當前時間數。
```php
date('Y-m-d H:i:s',strtotime('+1 day')); # 明日此時 (加減的都是此時,英文的都是零時)
date('Y-m-d H:i:s',strtotime('today 00:00:00')); # 今日零時
date('Y-m-d H:i:s',strtotime('next Thursday')); # 下周四零時
// year(年),month(月),hour(小時)minute(分),second(秒)
date('Y-m-d H:i:s',strtotime("+1 day +1 hour +1 minute")); # 一天一小時一分以后
date("Y-m-d H:i:s",strtotime("yesterday")); # 昨天零時
date("Y-m-d",strtotime("-2 day")); # 兩天之前
date('Y-m-d H:i:s',strtotime('+1 week')); # 下周此時
strtotime('+1 year',$dtStart); # 指定時間的1年后
// 第一天和最后一天
function getthemonth($date)
{
$firstday = date('Y-m-01', strtotime($date));
$lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
return array($firstday, $lastday);
}
/**
* 時間差計算
* @param Timestamp $time
* @return String Time Elapsed
* @author Shelley Shyan
* @copyright http://phparch.cn (Professional PHP Architecture)
*/
function time2Units($time)
{
$year = floor($time / 60 / 60 / 24 / 365);
$time -= $year * 60 * 60 * 24 * 365;
$month = floor($time / 60 / 60 / 24 / 30);
$time -= $month * 60 * 60 * 24 * 30;
$week = floor($time / 60 / 60 / 24 / 7);
$time -= $week * 60 * 60 * 24 * 7;
$day = floor($time / 60 / 60 / 24);
$time -= $day * 60 * 60 * 24;
$hour = floor($time / 60 / 60);
$time -= $hour * 60 * 60;
$minute = floor($time / 60);
$time -= $minute * 60;
$second = $time;
$elapse = '';
$unitArr = [
'年' => 'year' ,
'個月' => 'month' ,
'周' => 'week' ,
'天' => 'day' ,
'小時' => 'hour' ,
'分鐘' => 'minute' ,
'秒' => 'second'
];
foreach ($unitArr as $cn => $u) {
if ($$u > 0) {
$elapse = $$u . $cn;
break;
}
}
return $elapse;
}
$startTime = strtotime('2018-8-11 15:30:21');
$time = time() - $startTime;
echo time2Units($time); // 5分鐘
```
### date類庫
```php
// 獲取當前系統時間
$date = new DateTime();
$date->format('Y-m-d H:i:s');
//2、獲取特定時間并打印,同date(strtotime)一樣,英文的都到零時,加減的都是
$date2 = new DateTime('tomorrow');
$date2->format('Y-m-d H:i:s'); # 明日零時
$date2 = new DateTime('next Thursday');
$date2->format('Y-m-d H:i:s'); # 下周四零時
$date2 = new DateTime('+1 days');
$date2->format('Y-m-d H:i:s'); # 明日此時
$date = new DateTime();
$date->add(new DateInterval('P1Y')); // 一年后,
$date->format('Y-m-d H:i:s');
$date->modify('+1 day');
$date->format('Y-m-d H:i:s');
$date->setDate('1989','11','10');
$date->format('Y-m-d H:i:s'); // 某天此時
//3. unix時間戳的轉換
$date = new DateTime();
$date->format('U'); // 當前時間搓
$date->getTimestamp(); // 當前時間搓
$date->setTimestamp(1408950651);
$date->format('Y-m-d H:i:s');
//4. 日期的比較
$date1 = new DateTime();
$date2 = new DateTime('2016-12-15');
$date1 < $date2 ? '大' : '小';
$date1 = new DateTime();
$date2 = new DateTime('2018-8-10');
$diff = $date1->diff($date2);
//5. 兩個日期相差多少天
$interval = date_diff(date_create($startDate), date_create($endDate));
$interval->days;
//格式化輸出
echo $diff->format("The future will come in %Y years %m months and %d days");
// 生成每一天
$start = new \DateTime(date('Y-m-01',strtotime($item['the_start_date'])));
$end = new \DateTime('2021-12-10');
$interval = \DateInterval::createFromDateString('1 month');
$period = new \DatePeriod($start, $interval, $end);
foreach ($period as $dt) {}
```
~~~
// 構造函數參數 string $interval_spec,創建新的DateInterval對象
$datetime = new DateTime('2019-07-14 14:00:00');
$interval = new DateInterval('P2W'); //P2W 表示兩周,14天
dump($interval);
object(DateInterval)#227 (16) {
["y"] => int(0)
["m"] => int(0)
["d"] => int(14)
["h"] => int(0)
["i"] => int(0)
["s"] => int(0)
["f"] => float(0)
["weekday"] => int(0)
["weekday_behavior"] => int(0)
["first_last_day_of"] => int(0)
["invert"] => int(0)
["days"] => bool(false)
["special_type"] => int(0)
["special_amount"] => int(0)
["have_weekday_relative"] => int(0)
["have_special_relative"] => int(0)
}
// DateTime類的add方法:向DateTime對象添加天數、月數、年數、小時數、分鐘數和秒數
$datetime->add($interval);
echo $datetime->format('Y-m-d H:i:s'); //加14天, 2019-07-28 14:00:00
// DateTime類的sub方法:從DateTime對象中減去天、月、年、小時、分鐘和秒
$datetime->sub($interval); //減14天, 2019-07-14 14:00:00
echo $datetime->format('Y-m-d H:i:s');
// DateTime類的diff方法:返回表示為DateInterval的兩個DateTime對象之間的差異。返回DateInterval對象
$date1 = new DateTime("2019-11-04");
$date2 = new DateTime("2019-11-05");
$diff_interval = $date1->diff($date2);
dump($diff_interval);
// DateInterval 的format方法,格式參考‘DateInterval的format格式化參數’
echo $diff_interval->format('%R%a days'); // +1 days
// DateInterval:根據字符串創建DateInterval對象
// 以下每組的運行結果都相等
$i = new DateInterval('P1D');
$i = DateInterval::createFromDateString('1 day');
$i = new DateInterval('P2W');
$i = DateInterval::createFromDateString('2 weeks');
$i = new DateInterval('P3M');
$i = DateInterval::createFromDateString('3 months');
$i = new DateInterval('P4Y');
$i = DateInterval::createFromDateString('4 years');
$i = new DateInterval('P1Y1D');
$i = DateInterval::createFromDateString('1 year + 1 day');
$i = new DateInterval('P1DT12H');
$i = DateInterval::createFromDateString('1 day + 12 hours');
$i = new DateInterval('PT3600S');
$i = DateInterval::createFromDateString('3600 seconds');
~~~
構造函數參數 string?$interval\_spec 如:P2W。格式以字母P開頭,每個持續時間段由一個整數值表示,后跟一個周期標志符。 如果持續時間包含時間元素(表中的HMS),則在規范的該部分之前加上字母T。
| 周期標志符 | 描述 |
| --- | --- |
| *Y* | 年 |
| *M* | 月 |
| *D* | 日 |
| *W* | 周。這些被轉換成天,所以不能與\*D組合\*. |
| *H* | 小時 |
| *M* | 分鐘 |
| *S* | 秒 |
### Carbon
1. [官方文檔](https://carbon.nesbot.com/docs/)
2.
## 金額
### 顯示
```php
number_format(1000000,2); # 1,000,000.00
number_format(10000003.1485926,2); # 10,000,003.15 小數點使用了四舍五入
echo substr(number_format(10000003.1485926,3), 0 ,-1); # 10,000,003.14 不四舍五入
sprintf("%.2f",round($price,2))
```
### 計算
> PHP有精度運算bug,所以凡是精度運算,**特別是電商類價格運算**,都要使用擴展 `bcmatch`
> 另外,PHP7.3以前的round會有精度問題,需要再php.ini設置serialize\_precision = -1
#### 安裝
```bash
php -m | grep bcmath # 如果沒有
pecl install bcmath
```
#### 使用
```php
// 將兩個高精度數字相加
string bcadd(string left operand, string right operand [, int scale]);
// 比較兩個高精度數字,返回-1, 0, 1
int bccomp(string left operand, string right operand [, int scale]);
// 將兩個高精度數字相除
string bcdiv(string left operand, string right operand [, int scale]);
// 將兩個高精度數字相減
string bcsub(string left operand, string right operand [, int scale]);
// 求高精度數字余數
string bcmod(string left operand, string modulus);
// 將兩個高精度數字相乘
string bcmul(string left operand, string right operand [, int scale]);
// 求高精度數字乘方
string bcpow(string x, string y [, int scale]);
// 配置默認小數點位數,相當于就是Linux bc中的”scale=”
string bcscale(int scale);
// 求高精度數字平方根
string bcsqrt(string operand [, int scale]);
```
```php
// 左 - 右 保留兩位小數,結果無千分位格式
$res=bcsub(33344134.7,52.5,2);
echo $res;
```
- 現代化PHP特性
- php7常用特性整理
- 反射機制Reflection
- 依賴注入與服務容器
- 抽象類與接口
- 類多繼承的替代方案Traits
- 類的延遲綁定(后期綁定)
- 生成器語法
- 匿名函數和閉包
- 匿名類
- 理解php的output buffer
- 斷言ASSERT
- 魔術方法小結
- Zend Opcache字節碼緩存
- 內置的http服務器
- SPL標準庫
- 【SPL標準庫專題(1)】SPL簡介
- 【SPL標準庫專題(2)】Iterator
- 【SPL標準庫專題(3)】Classes
- 【SPL標準庫專題(4)】Exceptions
- 【SPL標準庫專題(5)】Datastructures:SplDoublyLinkedList
- 【SPL標準庫專題(6)】Datastructures:SplStack & SplQueue
- 【SPL標準庫專題(7)】Datastructures:SplPriorityQueue
- 【SPL標準庫專題(8)】Datastructures:SplHeap & SplMaxHeap & SplMinHeap
- 【SPL標準庫專題(9)】Datastructures:SplFixedArray
- 【SPL標準庫專題(10)】Datastructures:SplObjectStorage
- PHPcomposer使用手札[ing]
- PHP中的多態
- 通過命名空間實現自動加載的框架雛形
- 日期與金額
- PHPstorm使用攻略
- 筆記本