php - Find out if a date/time string contains a timezone -


i have table date/time information stored strings. due poor planning, of strings have timezone in them (e.g., "2014-01-13 04:22 -8") , others don't ("2014-01-13 04:22"). either type of string works fine strtotime() or new datetime() once that, information whether there timezone lost -- strings without timezone assigned server's default timezone. there function can use find out whether string contains timezone identifier?

it seems trivial write regex this, experience tells me date formats have enough variation , weird special cases it's better use built-in functions when possible.

you haven't lost information whether or not there timezone in date/time string once have created datetime object. if var_dump()on datetime object, output this:-

object(datetime)[1]   public 'date' => string '2014-01-13 04:22:00' (length=19)   public 'timezone_type' => int 1   public 'timezone' => string '-08:00' (length=6) 

as can see, timezone type , value of timezone passed both there, matter of fishing them out.

in case think need know if tz string passed in date/time string. if was, timezone type 1, if no string present, default system timezone have been used , of type 3.

you cannot timezone type directly, following function , can act accordingly.

function gettztype(\datetime $date) {     ob_start();     var_export($date);     $datestring = ob_get_clean();     preg_match('%=>.\d%', $datestring, $matches);     return (int)explode(' ', $matches[0])[1]; }  $date = new \datetime('2014-01-13 04:22 -8'); echo gettztype($date); 

the function returns int 1,2 or 3 corresponds time zone type explained in answer here.

see working.

this method allow avoid using regex on date/time strings, regex used on string known , reliable format.


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -