0

I've got this code:

<?php

$data = array(array("1","name1","2050-10-10"),array("2","name1","2051-10-10"));
$date = date("Y-m-d");
$x = 2;

foreach($data as $day)
{
$interval[] = abs(strtotime($date) - strtotime($day[$x]));
$interval[] = $day[$x];
}

var_dump($interval);

?>

I tried it on my own server :

It returns :

array(1) { 
[0]=> array(4) { 
  [0]=> int(1418338800) 
  [1]=> string(10) "2050-10-10" 
  [2]=> int(1418338800) 
  [3]=> string(10) "2051-10-10"    
} 
} 

but http://writecodeonline.com/php/ returns :

array(4) {
  [0]=>
  int(1130716800)
  [1]=>
  string(10) "2050-10-10"
  [2]=>
  int(1162252800)
  [3]=>
  string(10) "2051-10-10"
}

I don't understand why the value 1418338800 stays the same and I wonder how I could solve this problem!

Thank you a lot in advance

Following the @Jonathan Kuhn advice:

<?php

echo strtotime("2051-10-10");
echo strtotime("2050-10-10");
echo strtotime("2040-10-10");
echo strtotime("2039-10-10");
echo strtotime("2038-10-10");
echo strtotime("2037-10-10");
echo strtotime("2036-10-10");

?>

It returns:

empty line
empty line
empty line
empty line
empty line
2138738400
2107202400

The problem came from the 32 bit system.

Cœur
  • 37,241
  • 25
  • 195
  • 267
user1719210
  • 310
  • 3
  • 11
  • 1
    Your question indicates that there are two different dates being used, but in your code both of them say "2050-10-10". Are you sure you pasted the code correctly? – dg99 Dec 12 '14 at 00:25
  • 2
    Integer overflow maybe? 32 bit systems run out of timestamps on `03:14:07 UTC on 19 January 2038` – Jonathan Kuhn Dec 12 '14 at 00:28
  • Try checking that the timestamps for both dates aren't the same and what timestamp do you get for each date? – Jonathan Kuhn Dec 12 '14 at 00:32
  • @Jonathan Kuhn I didn't know about 32 bits systems timestamps limitation... Could I find a workaround? – user1719210 Dec 12 '14 at 00:32
  • From what I see the code works fine: http://codepad.viper-7.com/GD3PaX – Jonathan Kuhn Dec 12 '14 at 00:32
  • @user1719210 - workround is either to switch to 64-bit PHP, or to start using DateTime objects – Mark Baker Dec 12 '14 at 00:34
  • 1
    On a 32bit system, don't use timestamps for dates past 2038. You can use the datetime class. See: http://stackoverflow.com/questions/5319710/accessing-dates-in-php-beyond-2038 – Jonathan Kuhn Dec 12 '14 at 00:34
  • Thank you a lot !! Is there any other things I need to take care of on a 32 bit system? Is there a good documentation about the limitations? For anybody else having this issue, I found this : http://en.wikipedia.org/wiki/Year_2038_problem Thank you very much! – user1719210 Dec 12 '14 at 00:42
  • possible duplicate of **[PHP strtotime returning false for UTC time](http://stackoverflow.com/questions/19547043/php-strtotime-returning-false-for-utc-time)** – Glavić Dec 12 '14 at 06:49

0 Answers0