Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6174105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:41:42+00:00 2026-05-23T23:41:42+00:00

Alright, so i’m not sure if im converting user input time to GMT properly.

  • 0

Alright, so i’m not sure if im converting user input time to GMT properly. I will be having users across several timezones entering “events” and they will have to be able to see “how long untill” or “how long since” the current time();

This is how I was planning to convert the time they input. It will start as something like 07/21/2011 01:30 am Then,

echo gmdate('Y-m-d H:i:s',strtotime('07/21/2011 01:30 am'));

gives me 2011-07-21 08:30:00

So I was planning to take the value of gmdate('Y-m-d H:i:s',strtotime('07/21/2011 01:30 am')); then take time() and display “how long until this event” to users. But it seems like there is always 10 hours added onto the result, so if if i was scheduling an event 30 min from now it would say 10 hours 30 min from now. So, im thinking im not converting the local time correctly or something.

What am I missing? Maybe I just dont properly understand GMT. How can I make sure all the times involved are GMT so all times are universal to all the users on the website?

Other info if it helps:
The server timezone is America/Los_Angeles

EDIT:
After everyones suggestions i’ve tried setting this at the top of my php code:

date_default_timezone_set("GMT");

and I tried using date('Y-m-d H:i:s') to do the comparison to figure out the diff, but its saying “3 hours ago” rather than the 10 hours from now. So this definately changed things.

But still not correct.

I’ve confirmed date('Y-m-d H:i:s') is returning the correct and current GMT. So thats good.

But the user input date is off. How am I converting it incorrectly?


EDIT AGAIN(including some test results after Salman A’s suggestions):

2:55am – my current local time EST

date('Y-m-d H:i:s') shows up as 2011-07-21 06:56:43 – which is correct

3:00am EST is the time in the future I submitted as 07/21/2011 03:00 am

Here’s how I get the time “convert it” and submit it to my DB:

$time = $_POST['time'];

//there is where im assuming it turns my EST time to the GMT equivalent.
$the_date = strtotime($time . ' GMT');

$utctime = gmdate('Y-m-d H:i:s',$the_date);

I’m expecting my function to tell me the event is 5 minutes from now, but its hours off.

just to make sure the user submitted time was actually converted to GMT i display $utctime and it shows up as 2011-07-21 03:00:00 – which is not 08:00 or 07:00 (which i think one of those would be the GMT equivalent)

So how do I convert it?

So, what im seeing is strtotime($time . ' GMT'); doesn’t seem to be applying the GMT to the local time I supply. On a side note: somone suggested I have date_default_timezone_set("GMT"); in my code, so i have it at the top. Should I remove it? but i noticed if i remove it the GMT is incorrect. So thats why I left it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:41:42+00:00Added an answer on May 23, 2026 at 11:41 pm

    If you simply need to calculate the difference between two time values:

    <?php
    $time = '07/21/2011 11:30 am';
    $timeleft = strtotime($time) - time();
    // target time....: 2011-07-21 11:30:00 
    // current time...: 2011-07-21 11:13:45
    // difference.....: 975 seconds (16 min, 15 seconds)
    

    The above example assumes that $time has same timezone as that used by the time() function i.e. the server’s timezone.

    If the timezones differ, you must normalize them in order for subtraction to work as expected. So for example if you’re storing GMT date/time in your database then the above example becomes:

    <?php
    $time = '07/21/2011 06:30 am';
    $timeleft = strtotime($time . ' GMT') - time();
    // target time............: 2011-07-21 06:30:00 GMT
    // converted local time...: 2011-07-21 11:30:00 PKT
    // current time...........: 2011-07-21 11:34:48 PKT
    // difference.............: -288 seconds (minus 4 minutes, 48 seconds)
    

    Edit 1

    Regarding this code:

    $time = $_POST['time'];
    

    If your users are from various parts of the world, you should either:

    • ask them to enter the date/time in GMT
    • ask them to enter a timezone for the date entered

    You can later convert the date on server side and store it in database:

    <?php
    $source_time     = '2011-07-21 17:00';
    $source_offset   = '-0700'; // PDT
    $local_timestamp = strtotime($source_time . ' ' . $source_offset); // 2011-07-22 05:00 PKT (SERVER TIME)
    list(
        $temp_hh,
        $temp_mm
    )                = explode(':', date('P')); // returns difference between SERVER TIME and GMT
    $local_offset    = $temp_hh * 3600 + $temp_mm * 60;
    $gmt_timestamp   = $local_timestamp + $local_offset;
    echo date("Y-m-d H:i:s", $gmt_timestamp); // 2011-07-21 10:00:00
                                              // THIS is what you store in your database
                                              // Same as 2011-07-21 17:00:00 minus 7 hours
    

    Without the timezone information your calculations will be unreliable.

    Edit #2

    Actually… it is much simpler:

    <?php
    $source_time   = '2011-07-21 17:00';
    $source_offset = -7.0; // -0700
    echo date("Y-m-d H:i:s", strtotime($source_time) + $source_offset * 3600);
    // 2011-07-21 10:00:00
    // THIS is what you store in your database
    

    Edit #3

    <input type="text" name="time" id="time" value="07/21/2011 17:00">
    <input type="text" name="offset" id="offset">
    <script type="text/javascript">
        document.getElementById("time").onchange = function(){
        var d = new Date(this.value);
        alert('Date entered: ' + d + '\nDate to GMT: ' + d.toUTCString());
        }
        document.getElementById("offset").value = (new Date()).getTimezoneOffset() / 60;
    </script>
    

    Demo here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright. So I figure it's about time I get into unit testing, since everyone's
Alright, currently I have my SWF hitting a php file that will go and
Alright, quick question. A server is running in Eastern Time. PHP program needs to
Alright, I have been doing the following (variable names have been changed): FileInputStream fis
Alright. So I have a very large amount of binary data (let's say, 10GB)
Alright, I'm trying to read a comma delimited file and then put that into
Alright, after doing a ton of research and trying almost every managed CPP Redist
Alright, I know how the fieldset / legend works out in HTML. Say you
Alright. I have a query that looks like this: SELECT SUM(`order_items`.`quantity`) as `count`, `menu_items`.`name`
Alright, so I have a query that looks like this: SELECT `orders`.*, GROUP_CONCAT( CONCAT(

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.