为什么time()函数在不同的服务器上返回不同的值?

为什么time()函数在不同的服务器上返回不同的值?

问题描述:

When I write this line of code on local, the output will be this:

echo time(); // 1464605083

And when I write in on the 3v4l (online PHP shell) the output will be this:

echo time(); // 1339412843

As you see there is a huge different between those outputs. What's wrong? And how can I sync them?


Note: I'm using Xampp on my local.

That timezone will be vary according to the servers you are using. If you want to use same timezone in all your server. Use

date_default_timezone_set('timezonename')

The webserver where you are testing this has a completely off system time set.

If you look at the date of your test script (right next to the title field) you'll see that it has been saved at @ Mon Jun 11 2012 11:07:23.

Your timestamp 1339412843 translates to 06/11/2012 @ 11:07am (UTC).

So, the server time is just wrong. Test your script someplace else if time is critical.

use timezone i hope it's work ...

<?php 
 date_default_timezone_set('America/New_York');
echo $date= time() ;
 ?>

If the time you got back from the code is not the right time, it's probably because your server is in another country or set up for a different timezone.

So, if you need the time to be correct according to a specific location, you can set a timezone to use.

The example below sets the timezone to "America/New_York", then outputs the current time in the specified format:

<?php
date_default_timezone_set("America/New_York");
echo "The time is " . date("h:i:sa");
?>