---
layout: post
title: PHP 日期和数字
description: PHP语言简介
keywords: PHP语言简介
author: admin
date: 2018-07-01 15:49
category: 网络技术
tags: php
---
## 日期

```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')); # 下周此时



/**
* 时间差计算
* @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");
```


## 金额

### 显示

```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 不四舍五入
```

### 计算

> PHP有精度运算bug,所以凡是精度运算,**特别是电商类价格运算**,都要使用扩展 `bcmatch`


#### 安装

```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;
```