1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <code>
function createHisRange( $hisStart = '00:00:00' , $hisEnd = '23:59:59' , $range = 3600, $format = 'H:00:00' )
{
$t1 = strtotime ( $hisStart );
$t2 = strtotime ( $hisEnd );
$arrHis = array ();
while ( $t1 </code>
|
로그인 후 복사
로그인 후 복사
测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <code> $s = createHisRange();
print_r( $s );
</code>
|
로그인 후 복사
로그인 후 복사
问题
1 2 | <code>如何将 $format 传入到 array_map 当中
</code>
|
로그인 후 복사
로그인 후 복사
回复内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <code>
function createHisRange( $hisStart = '00:00:00' , $hisEnd = '23:59:59' , $range = 3600, $format = 'H:00:00' )
{
$t1 = strtotime ( $hisStart );
$t2 = strtotime ( $hisEnd );
$arrHis = array ();
while ( $t1 </code>
|
로그인 후 복사
로그인 후 복사
测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <code> $s = createHisRange();
print_r( $s );
</code>
|
로그인 후 복사
로그인 후 복사
问题
1 2 | <code>如何将 $format 传入到 array_map 当中
</code>
|
로그인 후 복사
로그인 후 복사
1 2 3 4 | <code> return array_map ( function ( $time ) use ( $format ) {
return date ( $format , $time );
}, range( strtotime ( $hisStart ), strtotime ( $hisEnd ), $range ));
</code>
|
로그인 후 복사
关键字:闭包
最优解当然是像 @picasso250 一样用use了
1 2 3 4 | <code> return array_map ( function ( $t ) use ( $f ) {
return date ( $f , $t );
}, range( strtotime ( $s ), strtotime ( $e ), $r ));
</code>
|
로그인 후 복사
如果用不了use关键字的话低版本的话还可以用global:
1 2 3 4 5 6 7 | <code> $format = "H:i:s" ;
function fm( $t ){
global $format ;
return date ( $format , $time );
}
array_map ( 'fm' , range( strtotime ( $hisStart ), strtotime ( $hisEnd ), $range ));
</code>
|
로그인 후 복사
当然用Class写这个也是可以的,就是看起来特别麻烦...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <code>Class hisRange {
public $s , $e , $r , $f ;
function __constructor( $s = '00:00:00' , $e = '23:59:59' , $r = 3600, $f = 'H:00:00' ) {
$this ->s = strtotime ( $s );
$this ->e = strtotime ( $e );
$this ->r = $r ;
$this ->f = $f ;
}
function run() {
return array_map ( array ( $this , 'fm' ), range( $this ->s, $this ->e, $this ->r));
}
function fm( $t ) {
return date ( $this ->f, $t );
}
}
$range = new hisRange();
$s = $range ->run();
</code>
|
로그인 후 복사
好吧,我大概只是来提供不同的解法的...