Martin AJ
Martin AJ

Reputation: 6697

Why time() function returns different values on different servers?

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.

Upvotes: 1

Views: 877

Answers (4)

User2403
User2403

Reputation: 317

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");
?>

Upvotes: 3

Kaja Mydeen
Kaja Mydeen

Reputation: 575

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')

Upvotes: 2

Nimesh Patel
Nimesh Patel

Reputation: 804

use timezone i hope it's work ...

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

Upvotes: 2

Gerald Schneider
Gerald Schneider

Reputation: 17797

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.

Upvotes: 1

Related Questions