We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

How do I retrieve the time using PHP

Bruce Gust
Bruce Gust asked
on
Medium Priority
308 Views
Last Modified: 2012-05-11
I'm using this:

      <?php
                  $b = time();
                  $the_time = date("g:i a", $b);
                  echo $the_time;
                  ?>

But the time, I'm assuming, is coming from the server that the code is hosted on. How do I grab the time from the user's computer?
Comment
Watch Question

vogenAnalyst Programmer
CERTIFIED EXPERT

Commented:
Juan OcasioContinuous Process Improvement Lead
CERTIFIED EXPERT

Commented:
Because PHP runs on the server, I do not believe you'll be able to retrieve the client's time unless you run javascript to retrieve that information and store it in a variable for your use.
You cannot grab( directly ) the user computer time in  PHP
YOu will need to grab his IP address  and then you will need to know the country based on the ip address .

another alternative is to use java script ( java script is a client side scripting so the time will be the computer time of the user not the server time )
vogenAnalyst Programmer
CERTIFIED EXPERT

Commented:
sorry didn't realise your are trying to get date from user pc.

vogenAnalyst Programmer
CERTIFIED EXPERT

Commented:
another alternative would be setting cookies
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
the Idea is you should have two files
timezone.js   ( that has the JavaScript )
and
timezone.php
that will get the user GMT offest form a session created by timezone.js
Bruce GustPHP Developer

Author

Commented:
Honestman - Here's what I'm doing based on your suggestion:


<!-- THREE STEPS TO INSTALL CURRENT TIME:

   1.  Paste the specified coding into the HEAD of your HTML document
   2.  Add the onLoad event handler to the BODY tag
   2.  Put the last code into the BODY of your HTML document  -->

<!-- STEP ONE: Copy this code into the HEAD of your HTML document  -->

<HEAD>

<SCRIPT LANGUAGE="JavaScript">

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
var timerID = null;
var timerRunning = false;
function stopclock (){
if(timerRunning)
clearTimeout(timerID);
timerRunning = false;
}
function showtime () {
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds()
var timeValue = "" + ((hours >12) ? hours -12 :hours)
if (timeValue == "0") timeValue = 12;
timeValue += ((minutes < 10) ? ":0" : ":") + minutes
timeValue += ((seconds < 10) ? ":0" : ":") + seconds
timeValue += (hours >= 12) ? " P.M." : " A.M."
document.clock.face.value = timeValue;
timerID = setTimeout("showtime()",1000);
timerRunning = true;
}
function startclock() {
stopclock();
showtime();
}
// End -->
</SCRIPT>

<!-- STEP TWO: Add this onLoad event handler to the BODY tag  -->

<BODY onLoad="startclock()">

<!-- STEP THREE: Copy this code into the BODY of your HTML document  -->

<CENTER>
<FORM name="clock">
<input type="text" name="face" size=13 value="">
</FORM>
</CENTER>

<?php
$time_now =

<!-- Script Size:  1.45 KB  -->


But how do I grab the time produced by the javascript and save it in a php variable. You'll see where I left off with $time_now = ... How do I finish this?
This is  not the code I mentioned to You .
The code provide seems to display time in the user browser and this time will move  every second , so now what I need to know is what exactly you want to store in PHP variable ?
is it the first time when the user first open the page ?

Commented:
The best option would be to set a cookie. Write current client's time using javascript and read this cookie through php.

Mat
Most Valuable Expert 2011
Author of the Year 2014
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.