How to display dates in the correct timezone

All dates which you receive and send through API are in the server's timezone (Post Affiliate Pro uses by default the  'America/Los_Angeles' timezone), but dates displayed in the merchant/affiliate panel are displayed in the client timezone (PC regional settings).
Here you can see some examples how to set the dates by API to be displayed in the desired timezone. 
 
First, you need to know your local timezone since it is needed for getting the time offset between your timezone and server's timezone. Here is an example how to get the time offset and set it as a constant 'TIME_OFFSET'. The time offset is in seconds (in the example as a local timezone the Europe/Bratislava timezone is used):
<?php

define('LOCAL_TIMEZONE', 'Europe/Bratislava');
define('TIME_OFFSET', getTimeOffset());

function getTimeOffset() {
    $dateTimeServer = date_create("now", timezone_open('America/Los_Angeles'));
    $dateTimeClient = date_create("now", timezone_open(LOCAL_TIMEZONE));
    return $dateTimeClient->getOffset() - $dateTimeServer->getOffset();
}

?>
 
Here are the functions for shifting the server time to the client time and the client time to the server time. These functions use the TIME_OFFSET and work with the time format as the Unix timestamp format:
<?php

function getClientTime($clientTimeStamp) {
    return $clientTimeStamp + TIME_OFFSET;
}

function getServerTime($serverTimeStamp) {
    return $serverTimeStamp - TIME_OFFSET;
}

?>
 
Function for converting timestamp to mysql datetime format (Y-m-d H:i:s):
<?php
function getDateTime($timestamp) {
    return date('Y-m-d H:i:s', $timestamp);
}

?>
 
Now, we can use all these functions  for shifting dates to mysql format (e.g.: 2011-04-20 or 2011-04-20 12:00:00), you can use it in filters and in display methods:
<?php
function dateTimeToClientTime($dateTime) {
    return getDateTime(getClientTime((strtotime($dateTime))));
}

function dateTimeToServerTime($dateTime) {
    return getDateTime(getServerTime((strtotime($dateTime))));
}

?>
 
Here you can see the whole example which filters and displays trasnsactions by date&time in the client timezone by using the  dateTimeToServerTime() and dateTimeToClientTime() functions:
 
<?php
include 'PapApi.class.php'; //the PapApi.class.php can be found at the PAP installation folder, in the 'api' directory

define('LOCAL_TIMEZONE', 'Europe/Bratislava');
define('TIME_OFFSET', getTimeOffset());

function getTimeOffset() {
    $dateTimeServer = date_create("now", timezone_open('America/Los_Angeles'));
    $dateTimeClient = date_create("now", timezone_open(LOCAL_TIMEZONE));
    return $dateTimeClient->getOffset() - $dateTimeServer->getOffset();
}

function getClientTime($clientTimeStamp) {
    return $clientTimeStamp + TIME_OFFSET;
}

function getServerTime($serverTimeStamp) {
    return $serverTimeStamp - TIME_OFFSET;
}

function getDateTime($timestamp) {
    return date('Y-m-d H:i:s', $timestamp);
}

function dateTimeToClientTime($dateTime) {
    return getDateTime(getClientTime((strtotime($dateTime))));
}

function dateTimeToServerTime($dateTime) {
    return getDateTime(getServerTime((strtotime($dateTime))));
}

$session = new Pap_Api_Session("https://www.yoursite.com/pap4/scripts/server.php");

if(!$session->login("merchant@username.com","merchant_password")) {
  die("Cannot login. Message: ". $session->getMessage());
}

$request = new Pap_Api_TransactionsGrid($session);

// set filter, this is same as if you set in merchant panel filter from 2011-04-11 to 2011-04-19 (value toDate is automatically converted to end of this day )$request->addFilter('dateinserted', 'D>=', dateTimeToServerTime('2011-04-11'));
$request->addFilter('dateinserted', 'D<=', dateTimeToServerTime('2011-04-19 23:59:59'));
$request->setLimit(0, 30);
$request->setSorting('orderid', false);


$request->sendNow();
$grid = $request->getGrid();

$recordset = $grid->getRecordset();

echo "Total number of records: ".$grid->getTotalCount()."<br>";
echo "Number of returned records: ".$recordset->getSize()."<br>";

// iterate through the records
foreach($recordset as $rec) {
  echo 'Transaction OrderID: '.$rec->get('orderid').', Commission: '.$rec->get('commission').', Date inserted(server time: '.$rec->get('dateinserted'). '): '.dateTimeToClientTime($rec->get('dateinserted')).'<br>';
}

?>
×