-1

I have a DB that shows when the user made the last login but then it shows 1542575966120. I wanted it to show so 18/11/2018 19:00

I tried using this in php

$intDate = "20". $ infologado ["lastlogin"];

$newDate = date ("d-m-Y", strtotime ($ intDate));

but I could not.

sorry for English

Difster
  • 3,264
  • 2
  • 22
  • 32
zlDeath
  • 13
  • 2

2 Answers2

0

So as @Taha Paksu had mentioned, these numbers are a timestamp (seconds since 1 January 1970). Try this code:

$intDate = 1542575966120;
$newDate = date('d/m/Y H:i', $intDate/1000);

It is in miliseconds, date function accepts seconds, thus the division by 1000. Also no need to put it into strtotime, because this function is meant to convert string dates to... said numeric timestamps.

In your case, you can put $intDate = $infologado['lastlogin']; instead of first line to get the result dynamically from the database.

p0358
  • 139
  • 1
  • 8
0

First of all, you need to learn what a timestamp is. The timestamp is a number which shows the seconds passed (or milliseconds, some include the milliseconds too) since epoch (01/01/1970). A general definition can be found here:

The Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), not counting leap seconds (in ISO 8601: 1970-01-01T00:00:00Z). Literally speaking the epoch is Unix time 0 (midnight 1/1/1970), but 'epoch' is often used as a synonym for 'Unix time'. Many Unix systems store epoch dates as a signed 32-bit integer, which might cause problems on January 19, 2038 (known as the Year 2038 problem or Y2038). The converter on this page converts timestamps in seconds, milliseconds and microseconds to readable dates.

Taken from: https://www.epochconverter.com/ a tool which you can convert your dates to/from timestamps or vice versa.

Then to answer your question, the system saved the dates as a timestamp to the database to (probably) bypass the formatting errors on each different system that uses it.

Nevermind, TL;DR:

The number shows Sunday, 18 November 2018 21:19:26.120 when you give it to the timestamp converter I mentioned above. With PHP, you can use:

$unixTimestamp = 1542575966120;
$dt = DateTime::createFromFormat("U.u", $unixTimestamp / 1000);
var_dump($dt);

to convert to PHP DateTime class, then you can use it in your application.

Community
  • 1
  • 1
Taha Paksu
  • 15,371
  • 2
  • 44
  • 78