Is there any reason why calling the function date_default_timezone_set() will cause a fatal error to appear?

The script is as taken from the Wrox Publishing book Beginning PHP6, Apache, MySQL Web Development.

<?php
	date_default_timezone_set('America/New_York');
	echo 'Today is ';
	echo date('F d');
	echo ', ';
	echo date ('Y');
?>

The error received is "PHP Fatal error: Call to undefined function: date_default_timezone_set()"

    You are probably using a version of PHP older than 5.1.0, which is when that function was introduced.

      The server I'm using is supposed to be running PHP 5.2.6 though. I don't have direct control over it however, so I can't directly check why it's not working.

      Are there any other factors that may cause this issue?

        You might want to make sure of the version:

        <?php
        echo "Version: " . phpversion();
        ?>
        

        It's possible to disable specific functions via php.ini, though I cannot imagine why anyone would disable this one.

          Aha! It's only version 4.4.7.

          Looks like I need to go give my hosting company a bit of a sharp tongue. XD Thanks for the tip.

            jshl;10924574 wrote:

            Aha! It's only version 4.4.7.

            Looks like I need to go give my hosting company a bit of a sharp tongue. XD Thanks for the tip.

            Some hosts run both PHP4 and PHP5, one as an Apache module and one as CGI. Usually one version is accessed via a ".php" file suffix and the other via ".php4" or ".php5", depending on which they consider (or you configure) to be the "default". So you may want to check your hosting FAQ -- or just ask them, if that's the case. Or you could try running the above test in a ".php5" file and see if you get lucky. 🙂

              Do I need to specifically rename the file as a .php5 file? I just tried to rename the file on the server, but got a file not found error...

              Basically, if I run a .php file and the server is running in this mode, I will receive a report that the version is actually lower than it actually is?

                If they are running the host this way, then you would need to actually rename the file with a ".php5" suffix, then access it from your browser using that new file name.

                Again, this is only a possibility, but it doesn't hurt to try it.

                  Looks like they aren't. Oh well. Looks like I have to ask them to upgrade the PHP version or something. Thanks for your help.

                    Write a Reply...