How to change format of time
Question by user1304197
I have a $_POST ($_POST[‘durationChosen’]) which is a time and its format is ’00 Hrs 00 Mins 00 Secs’. Now I still want to display this format in the textbox but on the next page I want $_POST[‘durationChosen’] I want the format to be ’00:00:00′. How can I do this?
An example is in the textbox I have ’01 Hrs 30 Mins 15 Secs’ but in next page I want it formatted so its ’01:30:15′. Can this be done?
Answer by MichaelRushton
Try this:
<?php
$array = sscanf($_POST['durationChosen'], "%u %s %u %s %u %s");
$time = date('H:i:s', strtotime($array[0] . ':' . $array[2] . ':' . $array[4]));
Edit to reflect comment of acceptance below:
<?php
$time = str_replace(array(' Hrs ', ' Mins ', ' Secs'), array(':', ':', ''), $_POST['durationChosen']);
Answer by Starx
You might want to read the manual. The problem with your coding is that, you are using short notation of time, which the data function does not understand. Read here
A simple way to get around this
$time = $_POST['durationChosen'];
$time= str_replace("Hrs", "Hour", $time);
$time= str_replace("Mins", "minutes", $time);
$time= str_replace("Secs", "seconds", $time);
$time = strtotime($time);
echo date($time, "H:i:s");