How can I generate month leading with zero like 01-12
.
Here is my code:
<?php for($m = 1;$m <= 12; $m++){ $month = date("F", mktime(0, 0, 0, $m));?>
<li><a href="<?php echo site_url('agenda/'.$tgl[0].'/'.$m);?>"><?php echo $month;?></a></li><?php } ?>
the output url is still 1-12
. I want it to appear as 01-12
.
use sprintf
<?php echo sprintf('%02d', $month); ?>
in your snippet
<?php for($m = 1;$m <= 12; $m++): ?>
<li>
<a href="<?php echo site_url("agenda/${tgl[0]}/" . sprintf('%02d', $m)); ?>">
<?php echo date("F", mktime(0, 0, 0, $m)); ?>
</a>
</li>
<?php endfor; ?>
how about using str_pad
It will pad the string with a character '0' to a length of 2.
<?php
for($m = 1;$m <= 12; $m++) {
$month = str_pad($m, 2, '0', STR_PAD_LEFT);
}
?>
<?php for($m = 1;$m <= 12; $m++){ $month = date("F", mktime(0, 0, 0, $m)); if ($month < 10) $month = "0".$month; ?>
使用sprintf()
可以实现这种格式,
<?php echo sprintf("%02d",$month); ?>
Try with
$month = date('m',mktime(0, 0, 0, $m)); //HERE give 'm' option
" m " Numeric representation of a month, with leading zeros 01 through 12
又短又快:(sprintf 很贵)
$month = ($m = date('m')) < 10 ? '0' . $m : $m;
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.