PHP How do I format a number to show the hundreds as zero if the number is less than 100?

StackOverflow https://stackoverflow.com/questions/22698399

  •  22-06-2023
  •  | 
  •  

문제

I'm calculating a countdown to a date. XXX Days Left. I'd like to show the hundreds place as zero if the total days left is less than 100.

This is where I'm at so far:

$now = time();
$your_date = strtotime(get_field('first_day'));
$datediff = abs($now - $your_date);
echo floor($datediff/(60*60*24));

Based on the target date, the output is 72. I'd like it to show 072.

Can someone help me with this?

도움이 되었습니까?

해결책

You can use str_pad()

echo str_pad($value, 3, "0", STR_PAD_LEFT);

Or sprintf()

echo sprintf("%03d", $value);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top