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

Changing the date

The server time is 5 hours behind. Is there a way of adding hours?

$date = date("Y-m-d H:i:s");

4 Answers

strtotime() gives you a number back that represents a time in seconds. To increment it, add the corresponding number of seconds you want to add. 5 hours = 60*60*5 = 18000, so...

$nowtime = date("Y-m-d H:i:s"); $date = date('Y-m-d H:i:s', strtotime($nowtime)+5*60*60); echo $date;

Thanks! :)

Hi, try to look in you Timezone setting in PHP.INI file - search for "date.timezone = "

I don't have that file.

Well... maybe i just did not get the point of quiestion, but I would rather correct the server time.

Yes you have it. If you use XAMP it is c:\xamp\php\php.ini for linux it depends on distribution, but it could be something like /etc/php5/apache2/php.ini

Cheers, M.

Just be aware that in PHP, you can only get the server's local time. If this site is live online, you may not have access to the correct INI file to change your server's timezone, especially if you are on a shared hosting plan.

If you're using a local server, or you have a VPS or a dedicated server, you should be able to set the server's timezone.

The problem is everybody is going to get that time, regardless of their time zone. That may be what you want, or it may not be. If you need to do something with the time and you want the time to be the correct time for the user, you can use the date object in JavaScript to do the same thing.