How do I retrieve the time using PHP

Posted on 2011-04-25
Last Modified: 2012-05-11
I'm using this:

                  $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?
Question by:brucegust
    LVL 2

    Expert Comment

    LVL 14

    Expert Comment

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

    Expert Comment

    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 )
    LVL 2

    Expert Comment

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

    LVL 2

    Expert Comment

    another alternative would be setting cookies
    LVL 10

    Assisted Solution

    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

    Hope it helps
    LVL 10

    Expert Comment

    the Idea is you should have two files
    timezone.js   ( that has the JavaScript )
    that will get the user GMT offest form a session created by timezone.js

    Author Comment

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


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


    <SCRIPT LANGUAGE="JavaScript">

    <!-- This script and many more are available free online at -->
    <!-- The JavaScript Source!! -->

    <!-- Begin
    var timerID = null;
    var timerRunning = false;
    function stopclock (){
    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() {
    // End -->

    <!-- 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  -->

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

    $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?
    LVL 10

    Expert Comment

    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 ?
    LVL 1

    Expert Comment

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

    LVL 107

    Accepted Solution

    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!

    Best regards, ~Ray

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to dynamically set the form action using jQuery.

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now