Jump to content
Larry Ullman's Book Forums

Date And Time Functions - Chapter 10


Recommended Posts

Hi All,

 

How are you? hope all is well with you. I would like to ask you a question about date and time function.

 

I am reading through Chapter 10.There's a function that sets the default timezone 'date_default_timezone_set( );'

 

I typed it as it was in the example this book introduced. Like this -> date_default_timezone_set('America/New_York'); and put this webpage onto my web server.

 

But it said :Fatal error: Call to undefined function: date_default_timezone_set() in /www/mascdesign_com/email/datetime.php on line 15

 

Mysql version on my server is 5.1 and I think it shouldn't have any problem..

 

I am attaching my web page codes down below.

 

-------------------------------------------------------------------------------------------------------------

 

<!DOCTYPE html>

<html dir="ltr" lang="en-US">

 

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>Contact Me</title>

 

</head>

 

<body>

<h1>Contact Me</h1>

<?php #script 10.1 - datetime.php

 

//set the default timezone;

date_default_timezone_set('America/New_York');

 

 

//check for form submission

if(isset($_POST['submitted'])) {

 

//minimal form validation

if(!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['comments'])) {

 

//create the body

 

$body="Name:{$_POST['name']}\n\n

Comments:{$_POST['comments']}";

 

 

//make it no longer than 70 characters long.

$body=wordwrap($body, 70);

 

//send the email

mail('service@mascdesign.com','Contact form submission', $body,"From: {$_POST['email']}");

 

//print a message

echo'<p><em>Thank you for contacting me at'.date('g:i a (T)').'on'.date('l F j, Y').'.I will reply some day.</em></p>';

 

//how long did it all take?

echo'<p><strong>It took'.(time() - $_POST['start']).'seconds for you to complete and submit the form.</strong></p>';

//Clear $_POST (so that the form is not sticky)

$_POST=array();

 

}else{

 

echo'<p style="font-weight:bold; color:#c00;">Please fill out the form completely</p>';

 

}

 

}// end of main isset() if

 

// create the HTML form:

?>

 

<p>Please fill out the this form to contact me.</p>

 

<form action="datetime.php" method="post">

<p>Subject: <input type="text" name="subject" size="30" maxlength="80" value="<?php if(isset($_POST['subject'])) { echo $_POST['subject'];}?>"/></p>

<p>Name: <input type="text" name="name" size="30" maxlength="60" value="<?php if(isset($_POST['name'])) {echo $_POST['name'];} ?>" /></p>

<p>Email Address: <input type="text" name="email" size="30" maxlength="80" value="<?php if(isset($_POST['email'])) {echo $_POST['email'];} ?>" /></p>

<p>Comments: <textarea name="comments" rows="5" cols="30" ><?php if(isset($_POST['comments'])) { echo $_POST['comments']; } ?></textarea></p>

<p><input type="submit" name="submit" value="Send"></p>

<input type="hidden" name="submitted" value="TRUE">

 

</form>

 

</body>

 

</html>

---------------------------------------------------------------------------------------------------------------------------------

 

It would be deeply appreciated if you can help me with this. Thanks in advance and look forward to hearing from you soon.

Link to comment
Share on other sites

It's the PHP version that matters, not MySQL. If your PHP version is greater than or equal to 5.1.0, you need to set the timezone. It's likely that the PHP version on the server is lower than the PHP version on your development machine. You could check the version, and use the date_default_timezone_set( ) function when the version of PHP is greater than 5.0:

if ((float) substr(phpversion(), 0, 3) > 5.0) {
date_default_timezone_set('America/New_York');
}

That will test your version, and invoke the date_default_timezone_set() function only when it's needed, so it will work on both your development machine and the server. If you are including a config file with each page, that would be a good place for it. Otherwise, a common header file would work.

  • Upvote 1
Link to comment
Share on other sites

Thank you very much, Paul.

 

I meant to say the PHP version on my server was greater than 5.0. Not MySQL.

Anyway, It works well. Thanks again.

 

If you don't mind, could you explain what this if "(float) substr(phpversion(), 0, 3) > 5.0" means?

It sounds like my php version on my server is less than 5.0.

Link to comment
Share on other sites

If you don't mind, could you explain what this if "(float) substr(phpversion(), 0, 3) > 5.0" means?

It sounds like my php version on my server is less than 5.0.

Sure. First, the substr (phpversion(), 0, 3) is just getting the first three characters of the PHP version. (float) then casts that value as a floating point number, since we will be comparing it to a numeric value with a decimal. I did that so that the greater than comparison will be comparing numbers to numbers, to make sure PHP isn't comparing strings. It might work without casting the version as a float, but I think it's best to be explicit about that. If you look at an ASCII chart you'll see that numbers have a lower ASCII value than alpha characters, so that's one way of making sure you are comparing apples to apples.

 

I have the same issue as you appear to. I develop on version 5.2.4, but my server is running 4.4.0.

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...