1

I want to convert a Java timestamp into a php timestamp. I have a java code that creates a JSON Object and send the content of this object to a php-page, which should store them into a database.

My problem is, that the timestamps are different.

Java:

Wed Dec 14 14:06:07 CET 2016

The output (as long converted):

1481720767015

On php I tried two different solutions:

PHP:

echo date('d.m.Y H:i:s', intval("1481720767015")/1000);
echo "<br>".gmdate('d.m.Y H:i:s', intval("1481720767015")/1000);

The output:

14.12.2016 08:06:07
14.12.2016 13:06:07

So how can I change the timezone to get the second output to the right time/timezone?

Micha93
  • 628
  • 1
  • 9
  • 22

2 Answers2

1

There is a similar problem here : PHP date(); with timezone?

And the solution is - you need to pass the timezone when creating the date object like below :

$tz = 'Europe/London';
$timestamp = time();
$dt = new DateTime("now", new DateTimeZone($tz)); //first argument "must" be a string
$dt->setTimestamp($timestamp); //adjust the object to correct timestamp
echo $dt->format('d.m.Y, H:i:s');
Community
  • 1
  • 1
Tushar
  • 3,022
  • 2
  • 26
  • 26
  • thank you. I found another solution: $time = gmdate('d.m.Y H:i:s', intval("1481720767015")/1000); $time = date('d.m.Y H:i:s', strtotime($time)+60*60); – Micha93 Dec 14 '16 at 13:30
  • Your example does not use any timezon but directly use some raw interval ;) – Tushar Dec 14 '16 at 13:32
0

thank you. I found another solution:

$time = gmdate('d.m.Y H:i:s', intval("1481720767015")/1000);
$time = date('d.m.Y H:i:s', strtotime($time)+60*60);
Micha93
  • 628
  • 1
  • 9
  • 22