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
  •  | 
  •  

Domanda

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?

È stato utile?

Soluzione

You can use str_pad()

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

Or sprintf()

echo sprintf("%03d", $value);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top