0

I have a field in my database called time. It contains UNIX timestamp. However the data type is a string and in php the date function expects an integer.

So i tried parsing the string to an integer but the value changes.

    $time = $value['time'];
    echo "Time string is $time<br/>";

    settype($time,'integer');
    echo "time intenger is".$time."<br/>";

This is the output i get

Time string is 1499327190163

time intenger is2147483647

What am i doing wrong here? Or is there a better way to convert timestamp to a date? Thanks

EDIT : I just read the manual on settype and it said Maximum value for "int" is PHP_INT_MAX. Perhaps this could be my issue, would it be alright to change this, or should there be any security related concern in changing the max integer value?

Haider Ali
  • 918
  • 2
  • 9
  • 26
  • AFAIK, unix timestamp just 10 digits. And you have 13 digits, I think you need to divide it to 1000 first – Dolly Aswin Jul 06 '17 at 07:56
  • @DollyAswin the timestamp was generated using javascript. – Haider Ali Jul 06 '17 at 07:58
  • as Dolly suggests, beware that timestamps can come in seconds or milliseconds, depending on the language/settings. Also, can your function support floats? they can have bigger "integer" values – Kaddath Jul 06 '17 at 07:59
  • @Kaddath i dont mind using floats but date() does not take a float. – Haider Ali Jul 06 '17 at 08:01
  • Any function that accepts an int will typically also accept a float, and in fact often also accepts a string. Yay dynamic typing and implicit conversions. – deceze Jul 06 '17 at 08:03
  • @deceze well i tried parsing it to a float, maybe it has an issue because my timestamp is in ms and than s. I will try with seconds and report back here. – Haider Ali Jul 06 '17 at 08:05
  • You want to change `PHP_INT_MAX`? Good luck with that. (This is a limitation of the CPU architecture, not a *setting*! If your CPU supports 64 bit (virtually all do nowadays), you need to recompile PHP as a 64 bit version.) – deceze Jul 06 '17 at 08:11

3 Answers3

2

This is because the range (upper limit of) of integer is:

2147483647

In layman terms, a cup can't hold water of a bucket.

32-bit builds of PHP:

Integers can be from -2,147,483,648 to 2,147,483,647 (~ ± 2 billion)

64-bit builds of PHP:

Integers can be from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (~ ± 9 quintillion)

Check this answer

Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59
0

1499327190163 appears to be a UNIX timestamp in milliseconds, whereas PHP date functions work in timestamps in seconds. The string vs. int issue is secondary since the value will be implicitly converted to an int as necessary.

The bigger issue is that the value surpasses the upper limit of a 32bit integer, yet you need to do a mathematical operation on it to turn it from milliseconds into seconds. Either:

  1. Do it in the database; just SELECT timestamp / 1000.

  2. Do it in PHP using bcdiv:

    $ts = bcdiv($timestamp, '1000', 0);
    
  3. Convert it to a float and divide it as float; though accuracy may suffer slightly.

  4. Just trim off the last 3 characters of the string, which essentially has the same effect; it just doesn't feel proper IMO.

Now you have a timestamp PHP's date functions can work with.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • the timestamp was generated in javascript, just a quick question. Should i be doing the division on miliseconds or force javascript to generate a timestamp in seconds – Haider Ali Jul 06 '17 at 08:06
  • The database should store the timestamp in a native `TIMESTAMP` or `DATETIME` column, then you wouldn't have this issue between PHP and the database at all. Then it just depends on how you define your REST interface, whether you want to standardise on accepting times in seconds, milliseconds, or in fact some ISO string representation. Make that decision *language neutral*, not based on what language prefers what representation. – deceze Jul 06 '17 at 08:09
-1

Javascript use 13 digits for unixtime, so you need to convert it 10 digits unixtime

<?php
$timeStr = "1499327190163";
$timeInt = (int) 1499327190163;
$timestamp = $timeInt / 1000;
$date = date("Y-m-d H:i:s", $timestamp);
echo $date;

$dateTime = new  \DateTime();
$dateTime->setTimestamp($timestamp);
echo $dateTime->format("Y-m-d H:i:s");
Dolly Aswin
  • 2,684
  • 1
  • 20
  • 23