I am not very familiar with the sprintf function in php but I want to use it to format time. I want to use it to format time this way 12:00. So if a user of my app puts in 1200, I want to pass the variable containing that value through sprintf (if this is possible) and add the colin. Any ideas of what this would look like?
[Resolved] SprintF formatting
// where $sUserString = time entered
$sTime = date('h:i', strtotime($sUserString));
See [man]date[/man] for details...
it's not really a problem of sprintf - u first need to detect that the user forgot the colon. Assuming the user has to type in two numbers that (2 digits max) separated by a colon, u could do it like this:
// $time should contain the user input
$parts = explode(":", strval($time));
if(sizeof($parts) < 2))
{
// Missing colon
$time = strval($time);
if(strlen($time) == 3)
{
$time = "0" . $time{0} . ":" .$time{1} . $time{2};
}
else if(strlen($time) == 4)
{
$time = $time{0} . $time{1} . ":" . $time{2} . $time{3};
}
else
{
// Invalid input, notify etc.
$time = "00:00";
}
}
else
{
$time = $parts[0] . ":" . $parts[1];
}
hth
Thank you both for the suggestions. I appricieate them very much.