I have 3 variables $y, $m and $d and I want to create a single variable $date with the date formatted as YYYY/MM/DD (with leading zeros on day and month). Is there a simple formula without having at least 2 if’s?
Thanks chaps
I have 3 variables $y, $m and $d and I want to create a single variable $date with the date formatted as YYYY/MM/DD (with leading zeros on day and month). Is there a simple formula without having at least 2 if’s?
Thanks chaps
Yep, learn how to use [fphp]sprintf[/fphp] and possibly datetime.
<?php
$y = 10; $m = 5; $d = 3;
echo sprintf('%04d/%02d/%02d', $y, $m ,$d); // outputs 0010/05/03
<?php
$y = 10; $m = 5; $d = 3;
$date = new DateTime("$y-$m-$d");
echo $date->format('Y/m/d'); // outputs 2010/05/03
You’ll notice the two respond to $y=10 differently.
Ah - many thanks!
current date formate:
<?php
echo date(“Y/m/d”) . “<br>”;
?>