Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Build a Simple PHP Application Getting Started with PHP Your First PHP File

Server sending back the wrong informaiton

What should I do about my MAMP server app if its giving back the wrong time when implementing the date function?

2 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi,

There is a couple of things you can do.

  1. You can use the date_default_timezone_set function to override the timezone
  2. You can alter the php.ini file in your MAMP installation and change date.timezone

Either way you can see the PHP supported timezones and set the value to whichever timezone matches your location.

If you don't know where the php.ini file is see the following link. http://stackoverflow.com/a/11691791

Ema Dan
Ema Dan
15,109 Points

I had this same problem, the timezone was wrong which made the date() time wrong. Here's what I did:

+1. Create a .php file in your htdocs folder, name it anything, for this example I'm calling it info.php... Now stick this inside:

<!DOCTYPE html>
<html>
<body>
<p>The current server date+time is: <?php echo date("l F d Y, h:i:s A T"); ?></p>
<?php phpinfo() ?>
</body>
</html>

+2. Now save it, and open info.php in a web browser: localhost/info.php You should see the long form of the date and time and right now it's still gonna be the wrong timezone. Below that, you should have a large amount of info and long tables. Find "Loaded Configuration File", it should be near the beginning. I think the value for it changes depending on what version PHP you're using (Don't ask, I don't actually know that much, lol)... Anyway, mine said: /Applications/MAMP/bin/php/php5.6.2/conf/php.ini

+3. Navigate to that directory it said (/Applications/MAMP/bin/php/php[YOUR-VERSION-#]/conf/php.ini) in Finder, and open that php.ini file in TextEdit, and scroll through until you find this section:

;;;;;;;;;;;;;;;;;;;
; Module Settings ;
;;;;;;;;;;;;;;;;;;;
[Date]
; Defines the default timezone used by the date functions
; Will be changed by MAMP to system timezone
date.timezone = "America/New_York"

+4. You need to change "America/New_York" to whatever your city is for the correct timezone. For whatever reason mine originally said "Europe/Berlin", so I changed it to my city. The list of cities is here: http://php.net/manual/en/timezones.php Look through and find yours or the closest one. Then change the date.timezone value inside the php.ini file. Remember to keep the quotes around the "Continent/City"! Save php.ini with your changes.

+5. Stop the MAMP servers. Restart the MAMP servers.

+6. Refresh the localhost/info.php file in your browser. The date and time and timezone should now be correct. Scroll down through all the tables to the "Date" section to double-check that it says your correct time zone. For example, my tables say:

*date*
date/time support_____________________enabled
"Olson" Timezone Database Version_____2014.7
Timezone Database_____________________internal
Default timezone______________________America/New_York
Directive-----------------Local Value-----------Master Value
date.default_latitude_____31.7667_______________31.7667
date.default_longitude____35.2333_______________35.2333
date.sunrise_zenith_______90.583333_____________90.583333
date.sunset_zenith________90.583333_____________90.583333
date.timezone_____________America/New_York______America/New_York

Hope this works for anyone else with this problem! :)