如果没有前导零的小时,分​​钟,秒php,如何格式化HH:MM:SS次

时间:2017-10-07 09:06:55

标签: php formatting timestamp

我有来自数组的时间字符串,如果不存在低于10的整数值,我想要添加前导零。 例如,我想要

this "8:10:12"  to "08:10:12"
this "13:7:14"  to "13:07:14"
this "13:25:6"  to "13:25:06"

我可以通过“:”等来分解一个非常混乱的代码,但是我想知道是否可以通过一行清理代码完成。如果单线无法做到,请回复。那我就把乱码。

提前致谢..!

3 个答案:

答案 0 :(得分:5)

您可以将php日期函数datestrtotime

一起使用
$str = "8:10:12";
$new_str = date("H:i:s",strtotime($str));
echo $new_str;

DEMO

答案 1 :(得分:2)

您可以使用explodearray_mapsprintfimplode使其成为单行,但它并不漂亮。

$time = implode(':', array_map(function($num) { return sprintf("%02d", $num); }, explode(':', $time)));

有关格式化前导零的数字的其他方法,请参阅Formatting a number with leading zeros in PHP

答案 2 :(得分:1)

由于这是非标准时间格式,您必须手动解析它:

_drawMaskLayer(changeMaterial=true) {
    if(this.lineMaskLayer.visible===false)return;

    //the length of "this.baseLine" is 1000
    this.lineMaskLayer.geometry=this.baseLine.geometry;
    this.lineMaskLayer.geometry.computeLineDistances();
    this.lineMaskLayer.geometry.lineDistancesNeedUpdate=true;
    if(changeMaterial) {
        this.lineMaskLayer.material = new THREE.LineDashedMaterial({
            color: this._properties.color,
            linewidth: this._properties.size,
            dashSize: 0.2 / this.cameraControl.getCurrentScale(),
            gapSize: 0.2 / this.cameraControl.getCurrentScale(),
            transparent: true,
            depthTest:false
        });
    }else{
        this.lineMaskLayer.material.dashSize=0.2 / this.cameraControl.getCurrentScale();
        this.lineMaskLayer.material.gapSize=0.2 / this.cameraControl.getCurrentScale();
        this.lineMaskLayer.material.needsUpdate=true;
    }
}

输出显然是:

 let vTubeDashedShader=[
'uniform vec3 color;',
'attribute vec3 center;',
'attribute float distance;',
'varying float vDistance;',
'varying vec3 vColor;',
'varying vec4 vPosition;',
'void main(){',
'   vec4 mvPosition=modelViewMatrix*vec4(position,1.0);',
'   gl_Position=projectionMatrix*mvPosition;',
'   gl_PointSize=1.0;',
'   vColor=color;',
'   vec3 test=vec3(center);',
'   vDistance=distance;',
'   ',
'}'
].join('\n');

let fTubeDashedShader=[
'uniform float dashSize;',
'varying float vDistance;',
'varying vec3 vColor;',
'varying vec4 vPosition;',
'void main(){',
'   float dev=floor(vDistance/dashSize);',
'   if(mod(dev,2.0)<0.01){',
'      discard;',
'   }',
'   gl_FragColor=vec4(vColor,1.0);',
'}'
].join('\n');