how to convert time with decimal point to time correctly using strtotime and date function?

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

  •  22-09-2022
  •  | 
  •  

I am trying to convert a 1.3 to 1:30 but when I use strtotime with date function 1.3 is becoming 1.03 !!! whats is the solution for this?

this is what I am using and giving me the unexpected result:

<?php $time_from = 1.3; ?>
<?= date('h:i A', strtotime($time_from)); ?>

print_r or $opening_hours_array:

Array ( [sunday_from] => 24.45 [sunday_to] => 1.3 [monday_from] => 2 [monday_to] => 2.15 [tuesday_from] => 3 [tuesday_to] => 3.15 [wednesday_from] => 4 [wednesday_to] => 4.15 [thursday_from] => 5 [thursday_to] => 5.15 [friday_from] => 6 [friday_to] => 6.15 [saturday_from] => 7 [saturday_to] => 1 )

what I am trying to do is show opening ours from and to:

 <?php
$days_of_week = array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
            for ($index = 0; $index < count($days_of_week); $index++) {
                $pos = strpos($opening_hours_array[strtolower($days_of_week[$index]) . '_from'], '.');
                if ($pos === false) {
                    $time_from = $opening_hours_array[strtolower($days_of_week[$index]) . '_from'].'.00';
                }else{
                    $time_from = $opening_hours_array[strtolower($days_of_week[$index]) . '_from'];
                }
                $pos = strpos($opening_hours_array[strtolower($days_of_week[$index]) . '_to'], '.');
                if ($pos === false) {
                    $time_to = $opening_hours_array[strtolower($days_of_week[$index]) . '_to'].'.00';
                }else{
                    $time_to = $opening_hours_array[strtolower($days_of_week[$index]) . '_to'];
                }
}
?>
有帮助吗?

解决方案 2

the solution would be:

$pos = strpos($opening_hours_array[strtolower($days_of_week[$index]) . '_from'], '.');
                if ($pos === false) {
                    $time_from = $opening_hours_array[strtolower($days_of_week[$index]) . '_from'].'.00';
                }else{
                    $time_from = $opening_hours_array[strtolower($days_of_week[$index]) . '_from'];
                    $time_from = (strlen($time_from)==3)?$time_from.'0':$time_from;
                }
                $pos = strpos($opening_hours_array[strtolower($days_of_week[$index]) . '_to'], '.');
                if ($pos === false) {
                    $time_to = $opening_hours_array[strtolower($days_of_week[$index]) . '_to'].'.00';
                }else{
                    $time_to = $opening_hours_array[strtolower($days_of_week[$index]) . '_to'];
                    $time_to = (strlen($time_to)==3)?$time_to.'0':$time_to;
                }

其他提示

As @Goikiu suggested the following code should do the trick

$time_from = 1.3;
$time_from = (strlen($time_from)==3)?$time_from.'0':$time_from;
echo date('h:i A', strtotime($time_from));

But there are other situations like 

$time_from = 1 ; 

The code needs to check for those as well.
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top