简体   繁体   English

在PHP中以小时,分钟和秒设置前导零

[英]Setting the leading zero in hours,minutes and seconds in php

I have a script which i want to use to generate some data from a certain date. 我有一个脚本,我想使用该脚本从某个日期生成一些数据。 The problem i am having is setting the leading zero for minutes,hours and seconds. 我遇到的问题是将分钟,小时和秒的前导零设置为零。

For instance 例如

<?php
for($i=0;$i<25;$i++){
    //echo $i.'<br/>';
    $start = "2012-10-01 $i:00:00";
    echo $start.'<br/>'; 
}
?>

I want all hours from 0 to 9 to have a leading zero. 我希望从0到9的所有小时数都具有前导零。 Is there a functions that can help me have the leading zero in hours,minutes or seconds?. 是否有功能可以帮助我在小时,分钟或秒前取得零?

Try str_pad 尝试str_pad

for($i=0;$i<25;$i++){
    $temp = str_pad($i,2,"0",STR_PAD_LEFT)
    $start = "2012-10-01 $temp:00:00";
    echo $start.'<br/>'; 
}

Demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM