简体   繁体   中英

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. Is there a functions that can help me have the leading zero in hours,minutes or seconds?.

Try 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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