0

I am saving the timestamp in SQL as bigint(20). The number is correct and in android or https://www.epochconverter.com it works fine.

However I am not able to create a date-string based on the timestamp received from database.

First of all, the timestamp seems to come from database as a String, so I can't just say echo date("d.m.Y, $timestamp). The result is the famous 31.12.1969.

So I tried echo date("d.m.Y, strtotime($timestamp)). However, even though strtotime is said to be able to convert almost everything to a timestamp, a simple String containing a timestamp is not possible. Results still remained on the last day of Brian Adams probably favorite year.

Some progress I made by casting the $timestamp to a float value like so: echo date("d.m.Y", floatval($timestamp));. However, now things got really confusing for me. I seemed to have successfully converted my timestamp, however, date() gave me the dates around 02.09.52299.

The timestamps I am using are timestamps of current time, e.g. 1588489252657, which currently leads to the date 23.03.52307.

So all I want is to come to a date based on the timestamp 1588489252657 to see the 03.05.2020 on my screen.

Thanks for any advice!

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Gerke
  • 926
  • 1
  • 10
  • 20
  • 1
    Convert that timestamp from milliseconds to seconds and you will get the right date – John Conde May 04 '20 at 18:05
  • From your own conversor: *Assuming that this timestamp is in milliseconds* – Álvaro González May 04 '20 at 18:06
  • Thanks for the two answers. The main point for me was that I had to transform the timestamp from milliseconds to seconds. After this using ```echo date("d.m.Y", floatval($timestamp) / 1000);``` worked fine – Gerke May 05 '20 at 03:35

2 Answers2

3
<?php

$timestamp = 1588489252657; // Timestamp in Milliseconds
$seconds = round($timestamp/1000, 0); // Timestamp Rounded to Seconds

$date = new DateTime();  // Create PHP DateTime Object
$date->setTimestamp($seconds); // Set the Timestamp
echo $date->format('Y-m-d H:i:s'); // Specify the Required Format

The answers are pretty much in the comment sections. But I have shared this answer since this is another approach in OOP fashion. You can leverage the power of PHP's DateTime Class.

PHP Official Documentation For DateTime Class Link Below: PHP DateTime Class

Harish ST
  • 1,475
  • 9
  • 23
  • 1
    Thanks for the two answers. The main point for me was that I had to transform the timestamp from milliseconds to seconds. After this using ```echo date("d.m.Y", floatval($timestamp) / 1000);``` worked fine – Gerke May 05 '20 at 03:35
2

You have to transform the timestamp to seconds first.

$timestamp = 1588489252657;
$dateInUnixSeconds = round($timestamp / 1000, 0);
$date = \DateTimeImmutable::createFromFormat('U', (string) $dateInUnixSeconds);

echo $date->format('d.m.Y');

PS: I recommend you to use the \DateTimeImmutable object to avoid mutability problems. https://github.com/Chemaclass/php-best-practices/blob/master/technical-skills/immutability.md

JesusValera
  • 629
  • 6
  • 14
  • Thanks for the two answers. The main point for me was that I had to transform the timestamp from milliseconds to seconds. After this using ```echo date("d.m.Y", floatval($timestamp) / 1000);``` worked fine. – Gerke May 05 '20 at 03:35