-2

Why will $time = time(); not show anything.

I really do not understand how to correct this?

Warning: Undefined array key 4
Warning: Undefined array key 5
Warning: Undefined array key 6
Warning: Undefined array key 2
Warning: Undefined array key 3
Warning: Undefined array key 1

Deprecated: gmmktime(): Passing null to parameter #1 ($hour) of type int is deprecated

No matter what I do to fix this error it goes away and then comes back!

I did research for days tried everything and when I think it is fixed I get the same error. This is the unmodified version of what I was trying to fix!

isset/array etc

function formatTimestamp($time, $format='', $dateonly='') 
{
    global $datetime, $locale, $userdata, $board_config;
    
    if(empty($format)): 
        if(isset($userdata['user_dateformat']) && !empty($userdata['user_dateformat'])): 
          $format = $userdata['user_dateformat'];
        elseif (isset($board_config['default_dateformat']) && !empty($board_config['default_dateformat'])): 
          $format = $board_config['default_dateformat'];
        else: 
          $format = 'D M d, Y g:i a';
        endif;
    endif;

    if(!empty($dateonly)): 
      $replaces = ['a', 'A', 'B', 'c', 'D', 'g', 'G', 'h', 'H', 'i', 'I', 'O', 'r', 's', 'U', 'Z', ':'];
      $format = str_replace($replaces, '', (string) $format);
    endif;

    if((isset($userdata['user_timezone']) && !empty($userdata['user_timezone'])) && $userdata['user_id'] != 1): 
      $tz = $userdata['user_timezone'];
    elseif (isset($board_config['board_timezone']) && !empty($board_config['board_timezone'])): 
      $tz = $board_config['board_timezone'];
    else: 
      $tz = '10';
    endif;

    setlocale(LC_TIME, $locale);

    if(!is_numeric($time)):
      preg_match('/(\d{4})-(\d{1,2})-(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})/', (string) $time, $datetime);
      $time = gmmktime($datetime[4],$datetime[5],$datetime[6],$datetime[2],$datetime[3],$datetime[1]);
    endif;

    $datetime = FormatDate($format, $time, $tz);

    return $datetime;
}

If all I need for the time string $time = time(); why would it keep showing that error?

  • Check the return value of `preg_match()` to see, if you actually have a match. Also, check with `var_dump()` the result of `$datetime`. – Progman Jan 14 '23 at 19:55

2 Answers2

1

You left it in the preg_match etc... You need to delete it or it will keep saying that!

function formatTimestamp($time, $format='', $dateonly='') 
{
    global $datetime, $locale, $userdata, $board_config;
    
    if(empty($format)): 
        if(isset($userdata['user_dateformat']) && !empty($userdata['user_dateformat'])): 
          $format = $userdata['user_dateformat'];
        elseif (isset($board_config['default_dateformat']) && !empty($board_config['default_dateformat'])): 
          $format = $board_config['default_dateformat'];
        else: 
          $format = 'D M d, Y g:i a';
        endif;
    endif;

    if(!empty($dateonly)): 
      $replaces = ['a', 'A', 'B', 'c', 'D', 'g', 'G', 'h', 'H', 'i', 'I', 'O', 'r', 's', 'U', 'Z', ':'];
      $format = str_replace($replaces, '', (string) $format);
    endif;

    if((isset($userdata['user_timezone']) && !empty($userdata['user_timezone'])) && $userdata['user_id'] != 1): 
      $tz = $userdata['user_timezone'];
    elseif (isset($board_config['board_timezone']) && !empty($board_config['board_timezone'])): 
      $tz = $board_config['board_timezone'];
    else: 
      $tz = '10';
    endif;

    setlocale(LC_TIME, $locale);

    //if(!is_numeric($time)):
    //  preg_match('/(\d{4})-(\d{1,2})-(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})/', (string) $time, $datetime);
    //  $time = gmmktime($datetime[4],$datetime[5],$datetime[6],$datetime[2],$datetime[3],$datetime[1]);
    //endif;
    $time = time();
    $datetime = FormatDate($format, $time, $tz);

    return $datetime;
}
1

This was my final Fix

function formatTimestamp($time, $format='', $dateonly='') 
{
    global $datetime, $locale, $userinfo, $board_config;
  
        if(isset($userinfo['user_dateformat']) && !empty($userinfo['user_dateformat'])): 
          $format = $userinfo['user_dateformat'];
        elseif (isset($board_config['default_dateformat']) && !empty($board_config['default_dateformat'])): 
          $format = $board_config['default_dateformat'];
        else: 
          $format = 'D M d, Y g:i a';
        endif;

    if(!empty($dateonly)): 
      $replaces = ['a', 'A', 'B', 'c', 'D', 'g', 'G', 'h', 'H', 'i', 'I', 'O', 'r', 's', 'U', 'Z', ':'];
      $format = str_replace($replaces, '', (string) $format);
    endif;

    if((isset($userinfo['user_timezone']) && !empty($userinfo['user_timezone'])) && $userinfo['user_id'] > 1): 
      $tz = $userinfo['user_timezone'];
    elseif (isset($board_config['board_timezone']) && !empty($board_config['board_timezone'])): 
      $tz = $board_config['board_timezone'];
    else: 
     $tz = '10';
    endif;

    setlocale(LC_TIME, $locale);

    if(!is_numeric($time)):
      # https://stackoverflow.com/questions/5145133/preg-match-for-mysql-date-format
      $adate= date_create($time); //date format that you don't want
      $mysqldate = $adate->format($format);//date format that you do want
      $datetime = $mysqldate;
      //preg_match('/(\d{4})-(\d{1,2})-(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})/', (string) $time, $datetime);
      //$time = gmmktime($datetime[4],$datetime[5],$datetime[6],$datetime[2],$datetime[3],$datetime[1]);
    else:
      $datetime = FormatDate($format, $time, $tz);
    endif;
    //
    return $datetime;
}