-1

I'm trying to receive data from input that I changed from "text" to the "time" selector. Now when the data goes in, it's generating the following error listed below. I understand why this is happening, but don't know where and how to change the code so that the code can process the PHP "time" input.

Error:

Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (013:59) at position 0 (0): Unexpected character' in C:\xampp\htdocs\index.php:240 Stack trace: #0 C:\xampp\htdocs\index.php(240): DateTime->__construct('013:59') #1 C:\xampp\htdocs\index.php(84): format_time('13:59') #2 {main} thrown in C:\xampp\htdocs\index.php on line 240

Relevant Code:

if($to){
  $s = (int)format_time($start);
  $e = (int)format_time($end);


function ct($time){
  $timezone_offset = 14400000; //EST vs UTC in ms
  return new MongoDB\BSON\UTCDateTime(strtotime($time)*1000 + $timezone_offset);
}

function now_in_mongo($offset = 0){
  return new MongoDB\BSON\UTCDateTime(time()*1000 + $offset*1000);
}

function format_time($time){
  if(strlen($time) != 6){
    $time = "0$time";
  }

  $date = new DateTime($time);
  $date->add(new DateInterval('PT4H'));
  return $date->format('His');
}
Aleks G
  • 56,435
  • 29
  • 168
  • 265
user1535982
  • 43
  • 1
  • 3

2 Answers2

0

I am not sure why you are checking the input for length under 6. A correct time input would have length of 5 (e.g 12:34), so at least you should use

if(strlen($time) != 5)

However this also may not always be correct - yet I'm not sure how you are constructing your $time string.

Aleks G
  • 56,435
  • 29
  • 168
  • 265
0

I think your time format should be like this xx:xx, i didnt tried your code but you can check the string to parse using a regex '/^\d{2}:\d{2}$/'

And use DateTime::CreateFromFormat to create ur date object.

Rahmouni Rabii
  • 934
  • 1
  • 7
  • 16