plugin development – How to render a time-of-day string like ’16:42′ with a site’s chosen time format?

Question

The trick here is using wp_date() in a peculiar way, giving it the time of day in seconds (as if it were 1970-01-01 16:42:18), then asking for it to be formatted in UTC.

$time="16:42:18";
if ( preg_match( '/^\d\d:\d\d:\d\d$/', $time ) ) {    //validate the $time
  $ts =  intval( substr( $time, 0, 2 ) ) * 3600;
  $ts += intval( substr( $time, 3, 2 ) ) * 60;
  $ts += intval( substr( $time, 7, 2 ) );
  if ( $ts >= 0 && $ts < 86400 ) {
     $time = wp_date( get_option( 'time_format' ), $ts, 'UTC' );
  }
}

0
O. Jones 5 months 2022-03-19T07:37:29-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse