最优解当然是像 @picasso250 一样用use了
return array_map(function ($t) use ($f) {
return date($f, $t);
}, range(strtotime($s), strtotime($e), $r));
如果用不了use关键字的话低版本的话还可以用global:
$format = "H:i:s";
function fm($t){
global $format;
return date($format, $time);
}
array_map('fm', range(strtotime($hisStart), strtotime($hisEnd), $range));
当然用Class写这个也是可以的,就是看起来特别麻烦...
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();
好吧,我大概只是来提供不同的解法的...