[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How do I retrieve the time using PHP

Posted on 2011-04-25
11
Medium Priority
?
296 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?
0
Comment
Question by:brucegust
11 Comments
 
LVL 2

Expert Comment

by:vogen
ID: 35463978
0
 
LVL 15

Expert Comment

by:Juan Ocasio
ID: 35464003
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.
0
 
LVL 10

Expert Comment

by:honestman31
ID: 35464005
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 )
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:vogen
ID: 35464027
sorry didn't realise your are trying to get date from user pc.

0
 
LVL 2

Expert Comment

by:vogen
ID: 35464031
another alternative would be setting cookies
0
 
LVL 10

Assisted Solution

by:honestman31
honestman31 earned 600 total points
ID: 35464047
as I mentioned earlier you should use JavaScript    Here is an axample of using PHP and java script to get the GMT time offset using AJAX

http://stackoverflow.com/questions/13/how-can-i-determine-a-web-users-time-zone

Hope it helps
0
 
LVL 10

Expert Comment

by:honestman31
ID: 35464053
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
0
 

Author Comment

by:brucegust
ID: 35464255
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?
0
 
LVL 10

Expert Comment

by:honestman31
ID: 35464357
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 ?
0
 
LVL 1

Expert Comment

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

Mat
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1400 total points
ID: 35466674
It's a common question, and there are some important subtexts to the answer.  Please read this article.  In it, you will find a tested and working example showing exactly how to do this.  You will also read the underlying principles about the DATETIME standards that can make your applications easier to get right!
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

Best regards, ~Ray
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month18 days, 4 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question