Formatting date whose timestamp exceeds the int limit

So, we have the following code:

date("Ymd",time()+60*365*24*60*60); 

The idea is that I have to make a forecast, and I have a result in the number of days that I have to add to the current date. The forecast is designed for the year 2060 or will pass it ... in a 64-bit environment that works, but not so much on a 32-bit one :)

any ideas?

10x

LE:

Ok, so I tried:

 $date = new DateTime(); // for PHP 5.3 $date->add(new DateInterval('P20000D')); // for PHP 5.2 $date->modify('+20000day'); echo $date->format('Ym-d') . "\n"; 

and he works

+4
source share
4 answers

See this:

http://www.infernodevelopment.com/forum/Thread-Solution-2038-PHP-Date-Bug-Y2-038K-UNIX-TIMESTAMP-BUG

 <?php // Specified date/time in your computer time zone. $date = new DateTime('9999-04-05'); echo $date->format('YM-j') .""; // Specified date/time in the specified time zone. $date = new DateTime('2040-09-08', new DateTimeZone('America/New_York')); echo $date->format('n / j / Y') . ""; // INPUT UNIX TIMESTAMP as float or bigint from database // Notice the result is in the UTC time zone. $r = mysql_query("SELECT date FROM test_table"); $obj = mysql_fetch_object($r); $date = new DateTime('@'.$obj->date); // a bigint(8) or FLOAT echo $date->format('Ymd H:i: sP') .""; // OR a constant greater than 2038: $date = new DateTime('@2894354000'); // 2061-09-19 echo $date->format('Ymd H:i: sP') .""; ?> 
+1
source

this works on my 32 bit system:

 $date = new DateTime("2071-05-26"); echo $date->format('Ymd H:i:s'); 

// I saw this in this question

+2
source

If you add full years / days / months, I suppose you could use simple arithmetic on them individually and then use checkdate() ( it claims to work until 32767 ) to confirm the result

 $date = array('Y' => date('Y'), 'm' => date('n'), 'd' => date('j')); // +60 years $date['Y'] += 60; if (checkdate($date['m'], $date['d'], $date['Y'])) { $fulldate = implode('-', $date); } 
0
source
 <?php // A dirty hack function bigdate_to_string($t_64bit) { $t_base = strtotime('2038-01-01 00:00:00 +0000'); $t_32bit = $t_64bit - $t_base; return date("Y", $t_32bit) + 68 . date("-md H:i:s", $t_32bit); } $t_64bit = 130 * 365 * 86400; // November 30, 2099 UTC echo bigdate_to_string($t_64bit) . "\n"; ?> 

Output:

 susam@swift :~$ php datehack.php 2099-11-30 05:30:00 

I subtract 68 years from the moment and add it back while printing formatted output.

0
source

All Articles