0

I would like display the right timestamp of my query. The date in my database is this one and date timestamp is set in Europe/Berlin. That is in my server setup and I always get this result.

2017-05-26 09:05:58

I want to display the date from my query. here is the code of my query

select logs.action_date_time from AuditLogs

How can I convert the logs.action_date_time date in my place here in Philippines? I supposed to see this result

2017-05-26T09:05:58+02:00

I search it in google the unix_timestamp but I don't see any sample that they use in select before from query. I'm using symfony for this. I just give summary of what the result of my above sample code.

const SELECT =
        'SELECT logs.id, logs.object_id AS objectId, logs.object_type AS objectType, logs.object_name AS objectName, '.'logs.action, logs.action_date_time AS actionDateTime, logs.action_status AS actionStatus, logs.subject, logs.user, '.'logs.description, logs.ip, logs.param03_value AS userId, logs.param04_value AS userDisplayName ';

    public function getNext($params)
    {
        // Hardcode for now
        $limit = $params['limit'] ? (int) $params['limit'] : self::DEFAULT_LIMIT;
        $limit++;

        $select = self::SELECT;
        $whereClause = $this->constructWhereClause($params);

        $sql = "
            $select FROM spoke.audit_logs AS logs
            $whereClause
            ORDER BY logs.action_date_time DESC, logs.id DESC
            LIMIT $limit";

        if ($params['cursor']) {
            $now = new \DateTime();
            $datetime = $now->getTimestamp();
            if (isset($params['datetime'])) {
                $datetime = $params['datetime'];
            }
            $whereClause = $this->constructWhereClause($params, 'logs1', false);
            $sql = "
                $select
                FROM spoke.audit_logs logs1, spoke.audit_logs logs
                WHERE logs1.id = logs.id 
                    $whereClause
                    AND logs1.action_date_time <= '$datetime'
                    AND (logs1.id < {$params['cursor']} OR logs1.action_date_time < '$datetime')
                ORDER BY logs1.action_date_time DESC, logs1.id DESC
                LIMIT $limit";
        }
        $stmt = $this->getEntityManager()->getConnection()->query($sql);

        return $stmt->fetchAll(\PDO::FETCH_ASSOC);

    }
user3818576
  • 2,979
  • 8
  • 37
  • 62

2 Answers2

1

You need to use MySQL's CONVERT_TZ function to convert the timezones, e.g.:

SELECT CONVERT_TZ(action_date_time, 'CEST', 'PHT') FROM AuditLogs;

Alternatively, you can provide time zone difference as well, e.g.:

SELECT CONVERT_TZ(action_date_time,'+01:00','+08:00') FROM AuditLogs;

Here's the documentation.

Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
1

To get a UTC datetime column as a UNIX timestamp

SELECT UNIX_TIMESTAMP(CONVERT_TZ(`utc_datetime`, '+00:00', @@session.time_zone)) FROM `table_name`

Source is here: Link

Nawin
  • 1,653
  • 2
  • 14
  • 23