将UNIX时间戳转换为本地时间(作为整数)

For a simple PHP graphing script, I need to align time values at local time days. When I do something like $timestamp % 86400 == 0 I get a break at 2 am every day because I'm in UTC +2 here. Also all my grid lines are at 2am, 5am, 8am etc. instead of 0am, 3am, 6am etc.

I understand that UNIX timestamp integer values are always in UTC and that there's date() and gmdate() and such, but I need an integer in local time to do arithmetics on it. How would I do that in PHP? Is there a function such as ToLocalTime() and ToUniversalTime() like in .NET?

To clarify the question, I have nothing to do with readable formatting of the time. No YYYY-MM-DD HH:MM:SS or the like, I just deal with numbers. The input is a number of seconds since epoch in UTC, aka the UNIX timestamp. What I need is a number of the same kind but not in UTC but local time. So I need to convert a UNIX timestamp integer into a local timestamp integer. Hope this is understandable and anybody can imagine what this could be used for (aligning grid lines in a time-axis graph at local time).

I ended up using this to determine the local time UTC offset:

$tzoffset = date('Z');

Then I just add or subtract that value to timestamps and treat them like UTC times but they are really local times. Technically they are always "off" the real time I intend to work with, but that shift corrects the difference between UTC and local time and lets me do the maths on local times in the "UTC space". When I'm done, I can transform the time back into "local space" for displaying. I could also use the gmdate function and get local time format.

$now = time() + $tzoffset;
echo gmdate('...', $now);   // Prints local time
if ($now % 86400 == 0 && gmdate('w', $now) == 1)
{
    echo 'Monday midnight local time';
}

Use the datetime class and set the date_default_timezone_set

date_default_timezone_set('Europe/Lisbon');
$date = new DateTime();
echo $date->getTimestamp();

Update based on your comments:

I have a timestamp that is not "now" and need to apply DST offset to it.

PHP takes care of DST. The necessary conversion rules are part of the PHP installation.


NOTE:

The unix timestamp isn't affected by a timezone settings. Setting the timezone only affects the interpretation of the timestamp value.

The best way to go is using DateTime API.
So you can handle timezones via DateTimeZone class, something like this:

$timezone = new DateTimeZone('UTC');
$UTCDate = DateTime::createFromFormat('Y-m-d H:i','2016-05-01 10:00', $timezone);

# by your local timezone
$newDate = $UTCDate->setTimeZone(new DateTimeZone('America/New_York'));

A possible implementation:

// Europe/Bucharest is GMT+2 (GMT+3 during DST)
$tz = new DateTimeZone('Europe/Bucharest');

$now = new DateTime('now', $tz);    // now (2016-05-01 14:50:23, local time)
$dayStart = clone $now;
$dayStart->setTime(0, 0, 0);    // start of today (2016-05-01 00:00:00, local time)
$dayEnd = clone $now;
$dayEnd->setTime(23, 59, 59);   // end of today (2016-05-01 23:59:59, local time)

// If you need the timestamps (to compare with MySQL TIMESTAMP columns)
printf("%d .. %d
", $dayStart->getTimestamp(), $dayEnd->getTimestamp());
// It prints: 1462050000 .. 1462136399


// If you need the dates in UTC timezone (to compare with MySQL DATETIME columns)
$tzUTC = new DateTimeZone('UTC');
$dayStart->setTimezone($tzUTC);
$dayEnd->setTimezone($tzUTC);
printf("%s .. %s
", $dayStart->format('Y-m-d H:i:s'), $dayEnd->format('Y-m-d H:i:s'));
// It prints: 2016-04-30 21:00:00 .. 2016-05-01 20:59:59

Update

In order to get the local time every three hours you can use the DatePeriod class:

// A 3 hours interval
$interval = new DateInterval('PT3H');
// A period that starts at today's 00:00:00 (local time) and ends at 23:59:59
$period = new DatePeriod($dayStart, $interval, $dayEnd);
// Iterate over it to get the moments every 3 hours
foreach ($period as $moment) {
    echo($moment->format('Y-m-d H:i:s')."
");
}

If you append this piece of code to the code above, you will get the dates represented in UTC. If you skip the block of the code that changes the timezones of $dayStart and $dayEnd then the code above will print:

2016-05-01 00:00:00
2016-05-01 03:00:00
2016-05-01 06:00:00
2016-05-01 09:00:00
2016-05-01 12:00:00
2016-05-01 15:00:00
2016-05-01 18:00:00
2016-05-01 21:00:00