Juni Brosas
Juni Brosas

Reputation: 1486

timezone offsets in php

I've a time table array where I need to check the time start and end dynamically depending on the user's time zone. I have solved it using date_default_timezone_set(); but my problem is I cannot change the day which is a statically pulled data.

Some of the time is a day before the user's current date, for instance 'America/New_York' to 'Asia/Singapore' is 3/11/2014 11:28 pm to 3/12/2014 11:28 am respectively.

 Array
  (
    [timeTable] => Array
    (
        [1] => Array
            (
                [0] => stdClass Object
                    (
                        [id] => 519
                        [user_id] => 213
                        [day] => 1
                        [time_slot_id] => 1
                        [start] => 1375099200
                        [end] => 1375101300
                    )

                [1] => stdClass Object
                    (
                        [id] => 520
                        [user_id] => 213
                        [day] => 1
                        [time_slot_id] => 12
                        [start] => 1375125600
                        [end] => 1375127700
                    )

            )

    )

 [sameDayCount] => 2
)

The approach I'm thinking is to get all the offsets that has a day before or a day after the current user`s day, and do day - 1 or day + 1.

How can I set the time and day to change base on user`s time zone being the day as a static data?

Any suggestions?

Upvotes: 0

Views: 60

Answers (1)

Joshua Bixler
Joshua Bixler

Reputation: 531

You can use the DateTime class to modify the date a certain time. https://www.php.net/manual/de/class.datetime.php

$datetime = new DateTime();
$datetime->setTimeStamp('1375125600');
$datetime->modify('+12 hours');

Or since it is already in unix time just add time to it.

$unixtime = 1375125600;
// seconds * minutes * hours
$unixtime += 60 * 60 * 12;
echo date("F j, Y, g:i a", $unixtime);

Upvotes: 2

Related Questions