I have a registration page on which I want to show the current location's time and date(not server's) . Other than this, when a user registers himself I want the date to be saved in Asia/Kolkata format. How can this be implemented in php ? I looked around but found only solution for saving the data in Asia/Kolkata format but not about displaying time as local time(not server's time). Any insight will be really helpful and appreciated.
-
https://stackoverflow.com/questions/2705067/how-can-i-get-the-users-local-time-instead-of-the-servers-time – mfadel Jan 23 '18 at 06:51
-
Does it have to be in PHP? if you use Javascript to determine the time on the user's machine, and send it to your server in your desired format, you automatically get the user's local time, and you can convert it (if you want) or take a new timestamp in your ddesired timezone – JoSSte Jan 23 '18 at 06:52
-
what you want ? your machine time? – Anand Pandey Jan 23 '18 at 06:54
-
@AnandPandey Yes I need user's system time whatever location it may be – Anand Jan 23 '18 at 07:02
-
@JoSSte I figured out that this will be implemented with JS. So looking forward to it – Anand Jan 23 '18 at 07:36
2 Answers
You cant get the local machine time in php because its a server side language. You should try javascript for this:
Just instantiate a new Date object
var now = new Date();
var today = now.toDateString();
alert(today);
That will create a new Date object with the client's local time.
If you want your desired output first you have to use jquery for timestamp
$(document).ready(function(e) {
var newDate = parseInt((new Date().getTime()/1000).toFixed(0));
});
It gives the timestamp in js so you have to use the 'newDate' value to below php script.
<?php
echo gmdate("l, j F, Y", $newDate);
?>

- 2,025
- 3
- 20
- 39
-
I tried this one. Thanks for your time. But this gives "Tue Jan 23 2018 12:26:47 GMT+0530 (India Standard Time)". I just want to get "Tue Jan 23 2018 ". Is this possible? Sorry I am naïve to JS. Something like "Tuesday, 23rd January , 2018" – Anand Jan 23 '18 at 07:24
-
-
Thanks fro your effort. But somehow its not in the desired format. As I mentioned I want the display to be in `Tuesday, 23rd January , 2018` format. – Anand Jan 23 '18 at 07:45
-
Thank you again. Resolved to an extent but not exactly what I wanted. Here is what I got `Tue Jan 23 2018` and I want it to be `Tuesday, 23rd January , 2018` – Anand Jan 23 '18 at 08:01
-
-
-
-
-
you can add as a query string of timestamp in current page then get by php – Anand Pandey Jan 23 '18 at 09:33
-
Well I tried your earlier code for displaying date as `Tue Jan 23 2018` . Unfortunately it does not changes when system time is changed. – Anand Jan 23 '18 at 09:58
$_SERVER is an array which holds information of headers, paths, script locations. Web server creates the entries in the array. This is not assured that every web server will provide similar information, rather some servers may include or exclude some information.
May Try To get Client Information:
$dt=$_SERVER['REQUEST_TIME'];
$dt = new DateTime("@$dt"); // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m-d H:i:s'); // output = 2017-01-01 00:00:00
//More Server Information
PHP : $_SERVER['REQUEST_TIME']
PHP : $_SERVER['PHP_SELF']
PHP : $_SERVER['argv']
PHP : Super global variable: $argc
PHP : $_SERVER['GATEWAY_INTERFACE']
PHP : $_SERVER['SERVER_ADDR']
PHP : $_SERVER['SERVER_NAME']
PHP : $_SERVER['SERVER_SOFTWARE']
PHP : $_SERVER['SERVER_PROTOCOL']
PHP : $_SERVER['REQUEST_METHOD']
PHP : $_SERVER['QUERY_STRING']
PHP : $_SERVER['HTTP_ACCEPT']
PHP : $_SERVER['HTTP_ACCEPT_CHARSET']
PHP : $_SERVER['HTTP_HOST']
PHP : $_SERVER['REMOTE_PORT']
PHP : $_SERVER['SCRIPT_FILENAME']
PHP : $_SERVER['SERVER_ADMIN']
PHP : $_SERVER['SERVER_PORT']
PHP : $_SERVER['SERVER_SIGNATURE']
PHP : $_SERVER['PATH_TRANSLATED']
PHP : $_SERVER['SCRIPT_NAME']
PHP : $_SERVER['SCRIPT_URI']
Ref http://php.net/manual/en/reserved.variables.server.php https://www.w3resource.com/php/super-variables/$_SERVER.php

- 2,411
- 2
- 26
- 35