Search code examples
phpconstantsmktime

PHP Defined Constant and mktime() Do Not Agree


I'm trying to establish a maximum timestamp setting for a given hosting environment and set a constant with that value. I wrote the following:

define('SC_TIME_END_OF_64', mktime(23, 59, 59, 12, 31, 9999));

If I then display the two values on a system known to have 64-bit integers and 64-bit time:

echo SC_TIME_END_OF_64 . ' ' . mktime(23, 59, 59, 12, 31, 9999);

I get:

253402300799 253402318799

My constant is 18000 seconds too small.

Then I tried:

if (mktime(23, 59, 59, 12, 31, 9999) == 253402318799) {

That conditional resolves to false. If I substitute 253402300799, it will resolve to true.

Finally, I tried:

$time_64 = mktime(23, 59, 59, 12, 31, 9999);
echo $time_64;

And I get the expected answer:

253402318799

I'm stumped. What don't I understand about constants?


Solution

  • I'm fairly certain this is a timezone issue. If you do the two statements right next to each other it works:

    define('SC_TIME_END_OF_64', mktime(23, 59, 59, 12, 31, 9999));
    echo SC_TIME_END_OF_64 . ' ' . mktime(23, 59, 59, 12, 31, 9999);
    //253402329599 253402329599
    

    http://sandbox.onlinephpfunctions.com/code/73e441ef17890152380b6c875157596e0287a1b6

    But if you change the timezone at some point in between you get different results:

    define('SC_TIME_END_OF_64', mktime(23, 59, 59, 12, 31, 9999));
    date_default_timezone_set('Europe/Amsterdam');
    echo SC_TIME_END_OF_64 . ' ' . mktime(23, 59, 59, 12, 31, 9999);
    //253402329599 253402297199
    

    http://sandbox.onlinephpfunctions.com/code/795bede12ac82b69b360bf8cae9bcd9eebc2ebf4

    Either standardize on a timezone (ideally UTC), or use gmmktime() instead.