Skip to content

Commit

Permalink
closure优化;完成time知识点梳理
Browse files Browse the repository at this point in the history
  • Loading branch information
YuanLianDu committed Dec 26, 2016
1 parent 12a54df commit 9311472
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
- [x] [PHP常用函数系列之字符串处理](https://github.com/YuanLianDu/YLD-with-Php/blob/master/articles/php/String%20Operation.md)
- [x] [PHP面向对象编程](https://github.com/YuanLianDu/YLD-with-Php/blob/master/articles/php/Object-Oriented%20Programming.md)
- [ ] PHP常用数组函数
- [ ] PHP时间处理相关函数及开发实战技巧
- [x] [PHP时间处理相关函数及开发实战技巧](https://github.com/YuanLianDu/YLD-with-Php/blob/master/articles/php/time.md)
- [ ] 正则
- [ ] 文件
- [x] [PHP 预定义接口--closure](https://github.com/YuanLianDu/YLD-with-Php/blob/master/articles/php/Closure.md)

- [x] [Exception 学习笔记](https://github.com/YuanLianDu/YLD-with-Php/blob/master/articles/php/Exception.md)
+ [ ] MongoDB
- [ ] 百万级数据处理

Expand Down
6 changes: 3 additions & 3 deletions articles/php/Closure.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ function echoStr(){
$invokeOutsideParam = function () use($num) {
echo $num;
$num ++;
};
};//使用use 调用匿名函数的外部参数
$invokeOutsideParam();
$invokeOutsideParam();//执行匿名函数
echo $num;
}
输出: 11
Expand All @@ -97,7 +97,7 @@ function echoStr(){
$num ++;
};
return $invokeOutsideParam;
return $invokeOutsideParam;//返回匿名函数
}
$echostr = echoStr();
Expand Down
104 changes: 104 additions & 0 deletions articles/php/time.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@


`date`开头的函数是过程化风格,还有相应的对象化风格。


## 开发实践

### 格式化时间
Expand All @@ -32,10 +34,112 @@
+ `date_format($date, 'G:ia');` 输出:`05:45pm`
+ `date_format($date, 'g:ia \o\n l jS F Y');` 输出:`5:45pm on Saturday 24th March 2012`

**Notice**:第一个参数$date,规定由`date_create()`返回的 DateTime 对象。

常用的格式化时间函数是,`date`,它可以将一个时间戳转化为我们想要的格式,并返回一个字符串,以下为各种格式的转化:

```
function dumpTime()
{
var_dump(date('Y-m-d H:i:s',time()));//string(19) "2016-12-26 11:31:37"
var_dump(date('d/m/Y H:i:s',time()));//string(19) "26/12/2016 11:31:37"
var_dump(date('d/m/y',time()));//string(8) "26/12/16"
var_dump(date('g:i A',time()));//string(8) "11:31 AM"
var_dump(date('G:ia',time()));//string(7) "11:31am"
var_dump(date('g:ia \o\n l jS F Y',time()));//string(36) "11:31am on Monday 26th December 2016"
}
```

### unix时间转化

`strtotime`将任何英文文本的日期时间描述解析为 Unix 时间戳:

+ `echo strtotime("1991-10-11 10:00:00")`
+ `echo strtotime("now")`
+ `echo strtotime("10 September 2000")`
+ `echo strtotime("+1 day")`
+ `echo strtotime("+1 week")`
+ `echo strtotime("+1 week 2 days 4 hours 2 seconds")`
+ `echo strtotime("next Thursday")`
+ `echo strtotime("last Monday")`


### 计算时间差

1、利用秒数,计算两个时间相差xx天xx小时xx分钟xx秒

```
function timeInterval() {
$time = strtotime('2016-11-11 13:10:11');
$timeOne = strtotime('2016-11-01 12:00:00');
$interval = $time - $timeOne;
$sec = $interval%60;
$minu = floor($interval/3600/24);
$hour = floor($interval%(3600*24)/3600);
$day = floor($interval/(3600*24));
echo $day.'day'.$hour.'hour'.$minu.'minu'.$sec.'sec';
}
```

输出:`10day1hour10minu11sec`


2、利用date对象,计算两个时间相差xx天xx小时xx分钟xx秒

```
function timeIntervalByDateObject() {
$timestamp = '1478833871';
$datetime = new DateTime(date('Y-m-d H:i:s', $timestamp));
$datetime_now = new DateTime();
$interval = $datetime_now->diff($datetime);
// var_dump($interval);
echo $interval->days.'day'.$interval->h.'hour'.$interval->i.'minu'.$interval->s.'sec';
}
```

输出:`45day4hour18minu7sec`;

打印的inter对象结构:
```
object(DateInterval)#3 (15) {
["y"]=>
int(0)
["m"]=>
int(1)
["d"]=>
int(15)
["h"]=>
int(3)
["i"]=>
int(55)
["s"]=>
int(23)
["weekday"]=>
int(0)
["weekday_behavior"]=>
int(0)
["first_last_day_of"]=>
int(0)
["invert"]=>
int(1)
["days"]=>
int(45)
["special_type"]=>
int(0)
["special_amount"]=>
int(0)
["have_weekday_relative"]=>
int(0)
["have_special_relative"]=>
int(0)
}
```

### 比较时间大小

利用`strtotime()`函数,将时间转化为时间戳。可以直接利用运算符,对两个时间戳进行比较~

### [时间人性化显示](https://segmentfault.com/q/1010000006702691)

```
Expand Down
66 changes: 66 additions & 0 deletions code/Time.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* Created by PhpStorm.
* User: yuan
* Date: 16/12/26
* Time: 11:09
*/

/*function transTime($ustime)
{
$ytime = date("Y-m-d H:i", $ustime);
$rtime = date("n月j日 H:i", $ustime);
$htime = date("H:i", $ustime);
$time = time() - $ustime;
$todaytime = strtotime("today");
$time1 = time() - $todaytime;
if ($time < 60) {
$str = '刚刚';
}else if ($time < 60 * 60) {
$min = floor($time / 60);
$str = $min . '分钟前';
}else if ($time < $time1) {
$str = '今天 ' . $htime;
}else {
$str = $rtime;
}
return $str;
}
var_dump(transTime('2016-12-26 03:01:52'));*/
function dumpTime()
{
var_dump(date('Y-m-d H:i:s',time()));
var_dump(date('d/m/Y H:i:s',time()));
var_dump(date('d/m/y',time()));
var_dump(date('g:i A',time()));
var_dump(date('G:ia',time()));
var_dump(date('g:ia \o\n l jS F Y',time()));
}
//dumpTime();

//时间差
function timeInterval() {
$time = strtotime('2016-11-11 13:10:11');
$timeOne = strtotime('2016-11-01 12:00:00');

$interval = $time - $timeOne;
$sec = $interval%60;
$minu = floor($interval/3600/24);
$hour = floor($interval%(3600*24)/3600);
$day = floor($interval/(3600*24));
echo $day.'day'.$hour.'hour'.$minu.'minu'.$sec.'sec';
}

function timeIntervalByDateObject() {

$timestamp = '1478833871';
$datetime = new DateTime(date('Y-m-d H:i:s', $timestamp));
$datetime_now = new DateTime();
$interval = $datetime_now->diff($datetime);
// var_dump($interval);
echo $interval->days.'day'.$interval->h.'hour'.$interval->i.'minu'.$interval->s.'sec';
}

timeIntervalByDateObject();

0 comments on commit 9311472

Please sign in to comment.