How to get server's timezone identifier dynamically in php?

2.4k Views Asked by At

I am printing time slots according some business hours in 30min difference to select start time of online appointment.

Am stuck on a problem, here brief explanation:

Bussiness Hours(start-end) = 10:00am - 5:00pm

Slot settings: 30min

Then slot listing will be display as:

10:00am - 10:30am - 11:00am - 11:30am - 12:00pm - 12:30pm - 01:00pm 01:30pm - 02:00pm - 2:30pm - 03:00pm - 03:30pm - 04:00pm - 04:30pm

I getting current server timezone time using hardcore $UserTimeZoneIdentifire timezone identifier variable and disabling all past time according current server timezone running time.

$UserTimeZoneIdentifire = "America/Denver";

For example:

If Customer 'ABC' current timezone time is: 12:33pm then all time slots between 10am-12:33pm will be disable and available time will be start from 12:35pm (rounding of minute in multiple of 5).

Here some code:

/**
 * If appointment date is current date then get current user's timezone time
 */

$UserTimeZoneIdentifire = "America/Denver";

if(strtotime($AppointmentDate) == strtotime(date("Y-m-d"))) {

    date_default_timezone_set($UserTimeZoneIdentifire);
    echo $Biz_start_time = $BusinessHours['Biz_start_time']; echo "--biz start time<br>";
    echo $UserCurrentTime = date("h:i A");  echo "--current server time<br>";

    echo $ServerCurMinute = substr($UserCurrentTime, 3,5);  echo "--strip minutes time<br>";
    echo $RoundOffMinute = round(($ServerCurMinute+5/2)/5)*5;   echo "--round off minutes time<br>";

    if($RoundOffMinute > 55) {
        /*
         * If minutes are greater then 55 then add 1 hour in time
         */
        $UserCurrentTime = substr_replace($UserCurrentTime, "00", 3, 2);
        $UserCurrentTime = date("h:i A", strtotime("+1 hour", strtotime($UserCurrentTime)));
    } else {
        if($RoundOffMinute <= 5) {
            $RoundOffMinute = "0".$RoundOffMinute;
        }
        $UserCurrentTime = substr_replace($UserCurrentTime, $RoundOffMinute, 3, 2);
    }
    $Biz_start_time = $UserCurrentTime;
    $Biz_end_time = $BusinessHours['Biz_end_time'];

} else {
    $Biz_start_time = "10:00am ";
    $Biz_end_time = "05:00pm";
}

Problem is, I have stored hard-coded value of my timezone identifier in a php variable. But, How could I get current server timezone identifier using php to set $UserTimeZoneIdentifire variable dynamically rather than hard coded value at any timezone?

So, any of help and other technique appreciated through you guys. Thanks.

0

There are 0 best solutions below