0

I get the value 9.0 from a CSV file that I would like to print as the time 09:00:00
If i try

<?php
$time = 9.01;
echo date('H:i:s', strtotime($time));

It displays 09:01:00.
But if I try

<?php
$time = 9;
echo date('H:i:s', strtotime($time)); or echo date('H:i:s', strtotime(9.00));

It displays 01:00:00. I want to get time 09:00:00.

Anthony
  • 2,014
  • 2
  • 19
  • 29
Deepak3301086
  • 447
  • 2
  • 23
  • 5
    Your problem is you are using a non standard format in _strtotime_ (that is supposed to take a string as parameter, not a double). – Anthony Aug 06 '18 at 12:33
  • Possible duplicate of [PHP strtotime() function that accepts a format?](https://stackoverflow.com/questions/5871963/php-strtotime-function-that-accepts-a-format) – Anthony Aug 06 '18 at 12:35
  • How are you reading the CSV? It sounds like somewhere along the line your data is being converted to an integer or float, which strtotime won't always be able to deal with. The string `'9.00'` should work fine. – iainn Aug 06 '18 at 12:40
  • As I saved xls to csv data automatically converted to float and integer according to value like 09.01 converted to 9.01 and 09.00 converted to 9 – Deepak3301086 Aug 06 '18 at 12:43
  • Is this the problem in my CSV file? – Deepak3301086 Aug 06 '18 at 12:50

4 Answers4

0

Please try this

$time =  9;
echo date( 'H:i:s', strtotime( number_format($time, 2)));
Midhun
  • 1,107
  • 10
  • 24
0

This is how to fill a date variable with any value you need:

<?php
$d=mktime(9, 0, 0);
echo "Created date is " . date("h:i:s", $d);
?>

See the documentation of mktime

Anthony
  • 2,014
  • 2
  • 19
  • 29
  • I tryed $t = '9'; $d = mktime($t); because my time come from csv, and it is also not work for me – Deepak3301086 Aug 06 '18 at 12:37
  • You need to fill all the values $d=mktime(hours, min, sec); or you can try this $d=mktime($t, 0, 0); – Ibrahim Abou Khalil Aug 06 '18 at 12:39
  • Do you means that, First i need to check value if float then explode and put in mktime($t[0], $t[1], 0) and if integer then put value same like mktime($t, 0, 0)? – Deepak3301086 Aug 06 '18 at 12:49
  • @Himas Not all the parameters are required, the documentation explains that they are optional. It's only forbidden to call it without *any* argument, we should use `time` instead. – Anthony Aug 06 '18 at 12:49
  • @AnthonyB yes true, but to be compatible with the date format we need to fill them. so that he can display {9:00:00} as desired. – Ibrahim Abou Khalil Aug 06 '18 at 12:51
  • @Himas I agree. It's up to the OP to check its date format and then to fill the arguments in mktime function. I think the example is now clear enough. – Anthony Aug 06 '18 at 12:53
  • @AnthonyB the last edit is good, but idk it's clear now, do u think it is better to approve it? – Ibrahim Abou Khalil Aug 06 '18 at 12:56
  • @Himas According to me (but I may be wrong), my last edit shows the time as it is from its CSV file, and how to get it working with `mktime`. I think it's relevant with the question. But it is your answer, it's up to you to accept or refuse my edit. – Anthony Aug 06 '18 at 12:58
0

It appears the main issue is the import CSV has a malformed date. Assuming you cannot change it and that the value is {hours}.{minutes}. Minutes not being a fraction but a value between 0 - 60. Otherwise, this will need adjusting to convert the fraction of an hour into minutes.

I'm using the DateTime class to achieve this.

$value = 9.1;

if (is_float($value)) {
    $hour = (int)$value;
    $minute = number_format(($value - floor($value)) * 100);

    $date = new DateTime();
    $date->setTime($hour, $minute);
} else {
    $date = new DateTime($value);
}

$formattedTime = $date->format('H:i');

echo $formattedTime . "\n";
Jason Grim
  • 413
  • 4
  • 7
-2

For 12 hours Formate echo date("h:i:s a"); For 24 hours Formate echo date("H:i:s");