30

How can I get the time of the client side? When I use date() it returns server's time.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Simon
  • 22,637
  • 36
  • 92
  • 121
  • 1
    The solution that worked for me was to set a cookie in the javascript and read it from PHP. Granted, you get the time from the previous page load. – Alesh Houdek Mar 11 '18 at 12:27
  • https://stackoverflow.com/a/62344221/12910765 this may help in showing php now time in visitor's time zone using php – Rana Nadeem Jun 12 '20 at 12:27

6 Answers6

40

Here's a "PHP" solution:

echo '<script type="text/javascript">
var x = new Date()
document.write(x)
</script>';
phimuemue
  • 34,669
  • 9
  • 84
  • 115
  • If a script is going to get executed on server, how come js knows the time on client pc. – Sachin Kanungo Nov 28 '15 at 00:28
  • Why i am get this error `Parse error: syntax error, unexpected T_ECHO` – Rajamohan S Sep 04 '16 at 01:32
  • You can take this solution and write it as a php function like this: `function the_local_time ($time) { ?> – mike Aug 08 '18 at 15:04
  • @SachinKanungo a tad late, but for others, this works as javascript is executed client side in the browser, the time it returns is whatever the client's time. PHP code on the other hand is executed on the server, and this you get the servers time. – Mint Feb 02 '21 at 07:13
31

As mentioned by everyone PHP only displays server side time.

For client side, you would need Javascript, something like the following should do the trick.

var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();

if (minutes < 10) {
    minutes = "0" + minutes;
}

document.write("<b>" + hours + ":" + minutes + " " + "</b>");

And if you want the AM/PM suffix, something like the following should work:

var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();

var suffix = "AM";

if (hours >= 12) {
    suffix = "PM";
    hours = hours - 12;
}

if (hours == 0) {
    hours = 12;
}

if (minutes < 10) {
    minutes = "0" + minutes;
}

document.write("<b>" + hours + ":" + minutes + " " + suffix + "</b>");

Here is a list of additional JavaScript Date and Time functions you could mess around with.

Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
Russell Dias
  • 70,980
  • 5
  • 54
  • 71
  • so the server sends a time header to the client but the client doesnt to the server? WHY? – My1 Oct 17 '17 at 12:41
  • https://stackoverflow.com/a/62344221/12910765 this may help in showing php now time in visitor's time zone – Rana Nadeem Jun 12 '20 at 12:27
4

As PHP runs on the server-side, you cannot access the client-side time from PHP : PHP doesn't know much about the browser -- and you can have PHP scripts that run without being called from a browser.

But you could get it from Javascript (which is executed on the client-side), and, then, pass it to PHP via an Ajax request, for example.


And here are a couple of questions+answers that might help you getting started :

Community
  • 1
  • 1
Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • well it might just have been possible that the client throws its time via a Header or whatever, the server does so as well after all. – My1 Oct 17 '17 at 07:36
4

You could possibly use Geolocation by IP Address to work out which country the user is in, and then use that.

But using Javascript or letting the user choose a Timezone will probably be better.

Jamescun
  • 673
  • 5
  • 8
0

PHP is server side only as far as i know.

You maybe want to use JavaScript.

phimuemue
  • 34,669
  • 9
  • 84
  • 115
0

As other's have mentioned, you can use Geo Location Services based on the IP Address.

I found it to be off by about 18 seconds due to IP location accuracy, by using a $responseTime offset it helped me narrow it down to 2 second accuracy in the Viewers Location.

  <?php; 

   echo deviceTime('D, M d Y h:i:s a');

   function deviceTime($dateFormatString)
   { 
        $responseTime = 21;
        $ip = (isset($_SERVER["HTTP_CF_CONNECTING_IP"])?$_SERVER["HTTP_CF_CONNECTING_IP"]:$_SERVER['REMOTE_ADDR']);
        $ch = file_get_contents('https://ipapi.co/'.$viewersIP.'/json/');
        $ipParts = json_decode($ch,true);
        $timezone = $ipParts['timezone'];
        $date = new DateTime(date('m/d/Y h:i:s a', time()+$responseTime));
        $date->setTimezone(new DateTimeZone($timezone));
    return $date->format($dateFormatString);
   }
   ?>
Empire of E
  • 588
  • 6
  • 12