我正在尝试编写一个函数,该函数返回人们在不同时区工作时是否在办公室。下面的代码似乎有效,但对我来说似乎很麻烦。我想知道是否有更好的方法。
<?
function isInoffice($remote_tz){
$office_hours = array(
'Monday' =>'9:00-17:00',
'Tuesday' =>'9:00-17:00',
'Wednesday' =>'9:00-17:00',
'Thursday' =>'9:00-17:00',
'Friday' =>'9:00-17:00',
'Saturday' =>'9:00-12:00',
'Sunday' =>'0:00-0:00'
);
$origin_tz = 'Australia/Melbourne';
$origin_dtz = new DateTimeZone($origin_tz);
$remote_dtz = new DateTimeZone($remote_tz);
$origin_dt = new DateTime("now", $origin_dtz);
$remote_dt = new DateTime("now", $remote_dtz);
$offset = $origin_dtz->getOffset($origin_dt) - $remote_dtz->getOffset($remote_dt);
$date = getdate();
$local = $office_hours[$date['weekday']];
$x = explode('-',$local);
$start = str_replace(':','',$x[0]);
$end = str_replace(':','',$x[1]);
$now = date('Hm',time()-$offset);
//echo "$start - $end - $now";
if( ($now>$start) && ($now < $end)){
return 1;
} else {
return 0;
}
}
echo isInoffice('America/New_York');
?>