Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

calculating epoch time from current date javascript php

Posted on 2010-11-26
16
1,115 Views
Last Modified: 2012-05-10
Dear Experts,

I am trying get the epoch time from the users current date. I believe I need to use javascript for this because it is coming from the users computer. I have found a few example scripts but my javascript skills are none.

What I am needing to do it to get the current date from the users computer say 1/1/2010 then I want to be able to convert this date into epoch time which would create the epoch time being 1291161600

the reason I believe I need to take it from the date is that I need the epoch time to not include the hours,minutes and seconds from the users computer only the epoch for the date (i.e if I converted the epoch time of 1291161600 back to readable date it would give me 01 Dec 2010 00:00:00. so the epoch created by the javascript would be the user current date at 00:00:00 midnight )

I have include the example codes I have found but I do not know what I am doing with it.

Many thanks as always for your help
<script type="text/javascript">

//set date var
d = new Date();

var day = d.getDate();
var year = d.getFullYear();
var month = d.getMonth() + 1;

//make date string
var date = year + "/" + month + "/" + day;

document.write(date);

</script>
<script> var myDate = ("date"); // Your timezone!
var myEpoch = myDate.getTime()/1000.0;
document.write(myEpoch);</script>

Open in new window

0
Comment
Question by:Lightwalker
16 Comments
 
LVL 20

Expert Comment

by:Mark Brady
ID: 34217160
Are you able to use php instead of javascript? If so, then get the current timestamp which is what I think you want by doing this:

$time = time();

That will give you a timestamp which will be a bunch of numbers
0
 
LVL 8

Expert Comment

by:kivan24
ID: 34217260
Place epoch time in hidden field:
<form id="form1" name="form1" method="post" action="index.php">
   <input name="epochtime" id="epochtime" type="hidden" value="">
   <script type="text/javascript">
       var dDate = new Date();
       var epoch = (dDate.getTime()-dDate.getMilliseconds())/1000;
       document.getElementById("epochtime").value = epoch;
   </script>
    ...........
   <input type="submit" value="Send">
</form>

Open in new window


After user click "Send" botton, use
$_POST['epochtime']

Open in new window

in PHP to get value of epoch time.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34218445
Have a look at this article:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

I'll see if I can post an example of how to do this in a moment.  kivan24 seems to have one good answer.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34219136
Install this and run it to see the moving parts, or use the copy on my server here:
http://www.laprbass.com/RAY_client_time.php?z=w

Then please post back if you have further questions about how this works.  Best regards, ~Ray
<?php // RAY_client_time.php
error_reporting(E_ALL);


// USE JAVASCRIPT TO GET THE CLIENT TIME AND COMPARE TO THE SERVER TIME


// TEST WITH DIFFERENT TIMEZONES IN THE URL PARAMETER
$z = isset($_GET["z"]) ? strtoupper(substr(trim($_GET["z"]),0,1)) : "?";
switch ($z)
{
    case 'W' : date_default_timezone_set('America/Los_Angeles'); break;
    case 'M' : date_default_timezone_set('America/Denver');      break;
    case 'C' : date_default_timezone_set('America/Chicago');     break;
    case 'E' : date_default_timezone_set('America/New_York');    break;
    default  : date_default_timezone_set('UTC');
}

echo "<pre>";

// IF THE FORM IS SUBMITTED
if (!empty($_POST))
{
    // ACTIVATE THIS TO SEE THE POST ARRAY
    var_dump($_POST);

    // SERVER SECONDS SINCE THE EPOCH
    $server_timestamp = time();

    // CLIENT'S JAVASCRIPT MILLISECONDS SINCE THE EPOCH
    $client_millitime = $_POST["date_u"];
    $client_timestamp = (int)round($client_millitime / 1000.0);
    $timestamps_diff  = $server_timestamp - $client_timestamp;
    echo "<br/>ACCORDING TO THE VALUE FROM dateObject.getTime()";
    echo "<br/>TIME FROM CLIENT PAGELOAD TO SERVER PROCESSING IS $timestamps_diff SECONDS";
    echo "<br/>CLIENT ISO8601 DATETIME IS " . date('c', $client_timestamp);
    echo PHP_EOL;

    // CLIENT'S OFFSET FROM GMT / UTC
    $client_offset_hours = (int)round($_POST["date_O"] / 60);
    echo "<br/>ACCORDING TO THE VALUE FROM dateObject.getTimezoneOffset()";
    echo "<br/>CLIENT IS LOCATED $client_offset_hours HOURS FROM UTC";
    echo PHP_EOL;

    // CLIENT'S DATE FROM THE POSTED STRINGS INTO A TIMESTAMP
    $client_timestamp = strtotime
    (       $_POST["date_Y"]
    . '-' . $_POST["date_m"]
    . '-' . $_POST["date_d"]
    . 'T' . $_POST["date_h"]
    . ':' . $_POST["date_i"]
    . ':' . $_POST["date_s"]
    )
    ;
    if ($client_timestamp === FALSE) echo "<br/>POST DATA IS BOGUS";

    echo "<br/>ACCORDING TO THE SEVERAL VALUES FROM dateObject.get***()";
    echo "<br/>CLIENT ISO8601 DATETIME IS " . date('c', $client_timestamp);
    echo "<br/>CLIENT PRETTY DATE IS " . date('l dS \o\f F Y g:i:s A', $client_timestamp);
    echo PHP_EOL;
}

// CURRENT SERVER INFORMATION
echo "<br/>THE SERVER TIMEZONE IS " . date_default_timezone_get();
echo "<br/>THE SERVER DATETIME IS " . date('c');

// END OF PHP - USE HTML AND JS TO CREATE THE FORM
echo "</pre>" . PHP_EOL; ?>

<form id="d" method="post">
<input name="date_O" id="dateTime_O" type="hidden" />
<input name="date_u" id="dateTime_u" type="hidden" />
<input name="date_Y" id="dateTime_Y" type="hidden" />
<input name="date_m" id="dateTime_m" type="hidden" />
<input name="date_d" id="dateTime_d" type="hidden" />
<input name="date_h" id="dateTime_h" type="hidden" />
<input name="date_i" id="dateTime_i" type="hidden" />
<input name="date_s" id="dateTime_s" type="hidden" />
<input type="submit" value="CHECK CLIENT DATE" />
</form>

<!-- NOTE THIS WILL GIVE YOU THE VALUES AT PAGE-LOAD TIME, NOT AT SUBMIT TIME -->
<!-- MAN PAGE REF: http://www.w3schools.com/jsref/jsref_obj_date.asp -->
<script type="text/javascript">
var dateObject = new Date();
document.getElementById("dateTime_O").value = dateObject.getTimezoneOffset();
document.getElementById("dateTime_u").value = dateObject.getTime();
document.getElementById("dateTime_Y").value = dateObject.getFullYear();
document.getElementById("dateTime_m").value = dateObject.getMonth() + 1;
document.getElementById("dateTime_d").value = dateObject.getDate();
document.getElementById("dateTime_h").value = dateObject.getHours();
document.getElementById("dateTime_i").value = dateObject.getMinutes();
document.getElementById("dateTime_s").value = dateObject.getSeconds();
</script>

Open in new window

0
 

Author Comment

by:Lightwalker
ID: 34219600
Thank you everyone for your guidance,

My apologies for my question, I was not very clear. kivan24 thank you for your post. I tested your reply and it produces the epoch for now i.e (1290803931) which is 27 November 2010 9:38:51 AM.

What I need is the epoch number that is produced is (epoch time is 1290816000) which is 27 November 2010 0:00:00 AM.

Many thanks
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34219740
You can use strtotime() and date() together to get the value you want.  Basically you want the epoch number for "today" instead of "now" because the "now" definition includes the time, whereas the "today" definition is the start of the day at midnight.  Did you read the article I pointed out yet?

Here is an example that I think will work for you.  Please install it and run it to see the moving parts.  Best, ~Ray
<?php // RAY_temp_lightwalker.php
error_reporting(E_ALL);
echo "<pre>";


// USE JAVASCRIPT TO GET THE CLIENT TIME AND COMPARE TO THE SERVER TIME


// REQUIRED FOR PHP 5.1+
date_default_timezone_set('America/Chicago');

// IF THE FORM IS SUBMITTED
if (!empty($_POST))
{
    // ACTIVATE THIS TO SEE THE POST ARRAY
    // var_dump($_POST);

    // CLIENT'S JAVASCRIPT MILLISECONDS SINCE THE EPOCH
    $client_millitime = $_POST["date_u"];
    $client_timestamp = (int)round($client_millitime / 1000.0);
    $client_date_only = date('Y-m-d', $client_timestamp);
    $client_date_only_timestamp = number_format(strtotime($client_date_only));
    echo "<br/>ACCORDING TO THE VALUE FROM dateObject.getTime()";
    echo "<br/>CLIENT ISO8601 DATETIME IS " . date('c', $client_timestamp);
    echo "<br/>CLIENT DATE ONLY IS $client_date_only";
    echo "<br/>CLIENT DATE ONLY TIMESTAMP IS $client_date_only_timestamp";
    echo PHP_EOL;
}

// END OF PHP - USE HTML AND JS TO CREATE THE FORM
echo "</pre>" . PHP_EOL; ?>

<form id="d" method="post">
<input name="date_u" id="dateTime_u" type="hidden" />
<input type="submit" value="CHECK CLIENT DATE" />
</form>

<!-- NOTE THIS WILL GIVE YOU THE VALUES AT PAGE-LOAD TIME, NOT AT SUBMIT TIME -->
<!-- MAN PAGE REF: http://www.w3schools.com/jsref/jsref_obj_date.asp -->
<script type="text/javascript">
var dateObject = new Date();
document.getElementById("dateTime_u").value = dateObject.getTime();
</script>

Open in new window

0
 

Author Comment

by:Lightwalker
ID: 34221092
Dear Ray,

Thank you so much for your examples. They were intimidating to me, but slowly piece by piece I am understanding  al little bit better how they work. Thank you so much for your replies. you always  help me understand another piece of the puzzle although I have a long way to go.

Happy Thanks giving by the way. I hope you have a wonderful weekend.

Thank you as well for the article you wrote. very helpful, but I am afraid I am still confused. I have run and tested both examples you sent me which work great but am still not sure how I can use the examples as my knowledge is still small If I explain what I am needing this for it might help.

I have a auto responder which I am using for a membership program. Each day a member receives an email before they wake up which contains the next part of there journey. Because of the time zone issues I want to be able to send the email between 2am and 4am ( I have done as to not worry so much about day light saving as the time does not need to be exact just early morning before the member wakes up). The auto responder runs from a CRON that is run from a epoch timestamp in a mysql table.

What I am wanting to do is feed an epoch timestamp into the DB that would represent 3 am on the same day they sign up. So that the first email they receive will be the next day at 3 am there local time.

My plan for doing is when the user is ready to being and a chooses to start today I get get an epoch time for there current date say today being 27 of november 2010 so the epoch timestamp will be 1290816000. this will give me a starting point. As I know that the epoch timestamp for the 27 of November at midnight. I have found a script which I was planning on using to help detect timezones but your script works much better to tell me the hours difference between the users local time and the server time (which my server is set to GMT/UTC) from this point I have a DB table which will show me if we are say -13 hours difference (being in New Zealand) then I will want to add or subtract ( depending on the timezone) a certain amount of hours to then make the epoch time the GMT equivalent of 3am of the users local time.

So it would look like:

Local epoch TODAY (- or +) mysql DB value matching the time difference (eg -13)

so an actual example for new zealand would be

1290816000 - 36000 = 1290780000 (the final epoch would represent 26 Nov 2010 14:00:00 GMT which the auto-responder would be able to use to send out the email at this correct time which is the same times as 27 November 2010 3:00:00 AM NZDT the users local time.

I am feeling stuck with this and would love some help with it. I am happy to pay you for your work if that's ok.

Again thank you so much for your help and happy thanks giving


0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34221804
Thanks for your good wishes.  

I think I can understand your needs, and it makes perfect sense to me.  Here is another code example that may be helpful to you.  Note the JavaScript dateObject.getTimezoneOffset() -- this seems to give the value you need to relate UTC time to client time.

This will only work if your server time is UTC.  I have not tested it exhaustively, but I believe that it is accurate.
<?php // RAY_temp_lightwalker.php
error_reporting(E_ALL);


// USE JAVASCRIPT TO GET THE CLIENT OFFSET FROM UTC
// ACHTUNG! THIS CODE ONLY WORKS FOR UTC, NOT OTHER TIMEZONES
date_default_timezone_set('UTC');


// IF THE FORM IS SUBMITTED
if (!empty($_POST))
{
    // JAVASCRIPT TELLS US THE CLIENT'S OFFSET FROM GMT / UTC
    $client_offset_minutes = $_POST["date_O"];

    // THE TIME WE WANT AT THE CLIENT LOCATION
    $client_time_text = 'TODAY 3:00AM';

    // MAKE THE COMPUTATIONS, INCORPORATING THE OFFSET FROM GMT
    $server_timestamp = strtotime($client_time_text) + ($client_offset_minutes * 60);
    $server_timestring = date('l, F j, Y \a\t g:i a', $server_timestamp);

    echo "<br/>ACCORDING TO THE VALUE FROM dateObject.getTimezoneOffset()";
    echo "<br/>CLIENT IS LOCATED $client_offset_minutes MINUTES FROM " . date_default_timezone_get();
    echo "<br/>IT IS $client_time_text AT THE CLIENT, WHEN IT IS $server_timestring IN " . date_default_timezone_get();
}

// END OF PHP - USE HTML AND JS TO CREATE THE FORM
echo PHP_EOL; ?>

<form method="post">
<input name="date_O" id="dateTime_O" type="hidden" />
<input type="submit" value="CHECK CLIENT DATETIME" />
</form>

<!-- NOTE THIS WILL GIVE YOU THE VALUES AT PAGE-LOAD TIME, NOT AT SUBMIT TIME -->
<!-- MAN PAGE REF: http://www.w3schools.com/jsref/jsref_obj_date.asp -->
<script type="text/javascript">
var dateObject = new Date();
document.getElementById("dateTime_O").value = dateObject.getTimezoneOffset();
</script>

Open in new window

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34221892
Here is a version that is generalized to get the client time offset from the server time.  Same concepts - relate both times to UTC to make the computations.
<?php // RAY_temp_lightwalker.php
error_reporting(E_ALL);


// USE JAVASCRIPT TO GET THE CLIENT TIME OFFSET FROM THE SERVER TIME


// LOCATION OF THE SERVER
date_default_timezone_set('America/Chicago');

// DIFFERENCE OF SERVER TIME FROM UTC
$server_offset_seconds = date('Z');

// IF THE FORM IS SUBMITTED
if (!empty($_POST))
{
    // JAVASCRIPT TELLS US THE CLIENT TIME OFFSET FROM GMT / UTC
    $client_offset_minutes = $_POST["date_O"];
    $client_offset_seconds = $client_offset_minutes * 60;

    // THE TIME WE WANT AT THE CLIENT LOCATION
    $client_time_text = 'TODAY 3:00AM';

    // MAKE THE COMPUTATIONS, INCORPORATING THE OFFSET FROM GMT
    $client_timestamp  = strtotime($client_time_text) + $client_offset_seconds;
    $server_timestamp  = $client_timestamp + $server_offset_seconds;
    $server_timestring = date('l, F j, Y \a\t g:i a', $server_timestamp);

    echo "<br/>ACCORDING TO THE VALUE FROM PHP date Z";
    echo "<br/>SERVER IS LOCATED $server_offset_seconds SECONDS FROM UTC";
    echo "<br/>";

    echo "<br/>ACCORDING TO THE VALUE FROM JS dateObject.getTimezoneOffset()";
    echo "<br/>CLIENT IS LOCATED $client_offset_minutes MINUTES FROM UTC";
    echo "<br/>";

    echo "<br/>WHEN IT IS '$client_time_text' AT THE CLIENT, IT IS '$server_timestring' IN " . date_default_timezone_get();
}

// END OF PHP - USE HTML AND JS TO CREATE THE FORM
echo PHP_EOL; ?>

<form method="post">
<input name="date_O" id="dateTime_O" type="hidden" />
<input type="submit" value="CHECK CLIENT DATETIME" />
</form>

<!-- NOTE THIS WILL GIVE YOU THE VALUES AT PAGE-LOAD TIME, NOT AT SUBMIT TIME -->
<!-- MAN PAGE REF: http://www.w3schools.com/jsref/jsref_obj_date.asp -->
<script type="text/javascript">
var dateObject = new Date();
document.getElementById("dateTime_O").value = dateObject.getTimezoneOffset();
</script>

Open in new window

0
 

Author Comment

by:Lightwalker
ID: 34223914
Dear Ray,

Thank you for your examples. I tested both of them on my server. I noticed that from New Zealand because of the international date line that if I run the script before 1pm then the calculation are a day out because of the 13 hour time difference, after 1 pm they are correct.

I added a little to your script to try and get the correct calculations using a switch statement to create variables called $client_seconds to store the number of seconds that need to be added or subtracted to the $today variable, as well as a variable called $plus to store a + or a - symbol.

But I do not know how to use the + symbol to add in the variable I created. at the moment it just displays it in  from <?php $userepoch = $today .$plus .$client_seconds ?> which outputs 1290902400-36000 instead of the correct calculation which would be 1290866400. Is this possible?

I know the code I added is clunky but this is the limit of my ability at the moment, but I would like to know if I can use a operator stored in  a variable.

Thank you again for your help which this. Sorry it is taking so long
<?php
// RAY_client_time.php
error_reporting(E_ALL);


// USE JAVASCRIPT TO GET THE CLIENT TIME AND COMPARE TO THE SERVER TIME


// REQUIRED FOR PHP 5.1+
date_default_timezone_set('GMT');
echo "<pre>";

// IF THE FORM IS SUBMITTED
if (!empty($_POST))
{
    // ACTIVATE THIS TO SEE THE POST ARRAY
    // var_dump($_POST);

    // SERVER SECONDS SINCE THE EPOCH
    $server_timestamp = time();

    // CLIENT'S JAVASCRIPT MILLISECONDS SINCE THE EPOCH
    $client_millitime = $_POST["date_u"];
    $client_timestamp = (int)round($client_millitime / 1000.0);
    $timestamps_diff  = $server_timestamp - $client_timestamp;
    echo PHP_EOL;

    // CLIENT'S OFFSET FROM GMT / UTC
    $client_offset_hours = (int)round($_POST["date_O"] / 60);
    echo "<br/>CLIENT IS LOCATED $client_offset_hours HOURS FROM UTC";
    echo PHP_EOL;

    // CLIENT'S DATE FROM THE POSTED STRINGS INTO A TIMESTAMP
    $client_timestamp = strtotime
    (       $_POST["date_Y"]
    . '-' . $_POST["date_m"]
    . '-' . $_POST["date_d"]
    . 'T' . $_POST["date_h"]
    . ':' . $_POST["date_i"]
    . ':' . $_POST["date_s"]
    )
    ;
    if ($client_timestamp === FALSE) echo "<br/>POST DATA IS BOGUS";
    echo "<br/>CLIENT PRETTY DATE IS " . date('l dS \o\f F Y', $client_timestamp);
    echo PHP_EOL;
}

// END OF PHP - USE HTML AND JS TO CREATE THE FORM

echo "</pre>" . PHP_EOL; ?>


<?php switch ($client_offset_hours) // Switch statement for how many seconds are added or subtracted from $today. $client_seconds is the number of seconds and $plus is either + or - (add or subtract)
{
case -13:
$client_seconds = 36000;
$plus = '-';       // This line will not be executed
break;

case -12:
$client_seconds = 32400;
$plus = '-';     // This line will not be executed
break;

case -11:
$client_seconds = 28800;
$plus = '-';      // This line will be executed
break;

case -10:
$client_seconds = 25200;
$plus = '-';        // This line will not be executed
break;

case -9:
$client_seconds = 21600;
$plus = '-';       // This line will not be executed
break;

case -8:
$client_seconds = 18000;
$plus = '-';       // This line will not be executed
break;

case -7:
$client_seconds = 14400;
$plus = '-';       // This line will not be executed
break;

case -6:
$client_seconds = 10800;
$plus = '-';       // This line will not be executed
break;

case -5:
$client_seconds = 7200;
$plus = '-';       // This line will not be executed
break;

case -4:
$client_seconds = 3600;
$plus = '-';       // This line will not be executed
break;

case -3:
$client_seconds = 0;
$plus = '-';       // This line will not be executed
break;

case -2:
$client_seconds = 3600;
$plus = '+';       // This line will not be executed
break;

case -1:
$client_seconds = 7200;
$plus = '+';       // This line will not be executed
break;

case 0:
$client_seconds = 10800;
$plus = '+';       // This line will not be executed
break;

case 1:
$client_seconds = 14400;
$plus = '+';       // This line will not be executed
break;

case 2:
$client_seconds = 18000;
$plus = '+';       // This line will not be executed
break;

case 3:
$client_seconds = 21600;
$plus = '+';       // This line will not be executed
break;

case 4:
$client_seconds = 25200;
$plus = '+';       // This line will not be executed
break;

case 5:
$client_seconds = 28800;
$plus = '+';       // This line will not be executed
break;

case 6:
$client_seconds = 32400;
$plus = '+';       // This line will not be executed
break;

case 7:
$client_seconds = 36000;
$plus = '+';       // This line will not be executed
break;

case 8:
$client_seconds = 39600;
$plus = '+';       // This line will not be executed
break;

case 9:
$client_seconds = 43200;
$plus = '+';       // This line will not be executed
break;

case 10:
$client_seconds = 46800;
$plus = '+';       // This line will not be executed
break;

case 11:
$client_seconds = 50400;
$plus = '+';       // This line will not be executed
break;

case 12:
$client_seconds = 54000;
$plus = '+';       // This line will not be executed
break;
}
?><br />
The number of seconds that need to be added or subtracted from the $epochcalc =
<?php echo $client_seconds ?><br /><br />
<?php $epochcalc = $_POST["date_Y"].- $_POST["date_m"].- $_POST["date_d"]?>
Date for Today from local =
<?php echo $epochcalc ?><br /><br />
<?php $today = strtotime($epochcalc);?>
epoch Date for Today from local =
<?php echo $today ?><br /><br />
<?php $userepoch = $today .$plus .$client_seconds ?>
Epoch Date calculations of $today - or + $client_seconds =
<?php echo $userepoch ?><br /><br />
<form id="d" method="post">
<input name="date_O" id="dateTime_O" type="hidden" />
<input name="date_u" id="dateTime_u" type="hidden" />
<input name="date_Y" id="dateTime_Y" type="hidden" />
<input name="date_m" id="dateTime_m" type="hidden" />
<input name="date_d" id="dateTime_d" type="hidden" />
<input name="date_h" id="dateTime_h" type="hidden" />
<input name="date_i" id="dateTime_i" type="hidden" />
<input name="date_s" id="dateTime_s" type="hidden" />
<input type="submit" value="CHECK CLIENT DATE" />
</form>



<!-- NOTE THIS WILL GIVE YOU THE VALUES AT PAGE-LOAD TIME, NOT AT SUBMIT TIME -->
<!-- MAN PAGE REF: http://www.w3schools.com/jsref/jsref_obj_date.asp -->
<script type="text/javascript">
var dateObject = new Date();
document.getElementById("dateTime_O").value = dateObject.getTimezoneOffset();
document.getElementById("dateTime_u").value = dateObject.getTime();
document.getElementById("dateTime_Y").value = dateObject.getFullYear();
document.getElementById("dateTime_m").value = dateObject.getMonth() + 1;
document.getElementById("dateTime_d").value = dateObject.getDate();
document.getElementById("dateTime_h").value = 00;
document.getElementById("dateTime_i").value = 00;
document.getElementById("dateTime_s").value = 00;
</script>

Open in new window

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34223994
Times are relative to UTC (also known as Greenwich Mean Time).  Dates are different at certain times because of this.  Why are you adjusting these values?
0
 

Author Comment

by:Lightwalker
ID: 34224197
The reason is and I am not sure why. If the time in New Zealand is 11AM on November the 28th 2010, the $server_timestring generated the date Friday, November 26, 2010 at 2:00 pm IN UTC.

the date Friday, November 26, 2010 at 2:00 pm IN UTC which when converted back to New Zealand time is 27 November 2010 3:00:00 AM NZDT which is a day out.

But because of the international date line if the time in New Zealand is 1PM November 28th 2010  the $server_timestring generated the date Saturday, November 27, 2010 at 2:00 pm IN UTC

this date is correct when converted back to new zealand time which is 28 November 2010 3:00:00 AM NZDT.

So the international date line is cause the calculation to be a day out. That the only reason I am adjusting these values.

Is it possible to use a variable as an operator to make the calculation?

Many thanks
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34225822
All of these look correct to me.
<?php // RAY_temp_lightwalker.php
error_reporting(E_ALL);

$locations
= array
( 'Pacific/Auckland'
, 'Australia/Sydney'
, 'Australia/Perth'
, 'Asia/Tokyo'
, 'Asia/Calcutta'
, 'Asia/Tel_Aviv'
, 'Africa/Cairo'
, 'Europe/Rome'
, 'Europe/London'
, 'Atlantic/Bermuda'
, 'America/Chicago'
, 'America/Anchorage'
, 'Pacific/Honolulu'
, 'UTC'
)
;

echo "<pre>";
foreach ($locations as $location)
{
   date_default_timezone_set($location);
   echo PHP_EOL . str_pad($location, 24, ' ');
   echo date('r');
}

Open in new window

0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 34225836
And perhaps this way of looking at it gives a better perspective.  The number of seconds since the UNIX epoch is the same, no matter where in the world you are.
<?php // RAY_temp_lightwalker.php
error_reporting(E_ALL);

$locations
= array
( 'Pacific/Auckland'
, 'Australia/Sydney'
, 'Australia/Perth'
, 'Asia/Tokyo'
, 'Asia/Calcutta'
, 'Asia/Tel_Aviv'
, 'Africa/Cairo'
, 'Europe/Rome'
, 'Europe/London'
, 'Atlantic/Bermuda'
, 'America/Chicago'
, 'America/Anchorage'
, 'Pacific/Honolulu'
, 'UTC'
)
;

echo "<pre>";
foreach ($locations as $location)
{
   date_default_timezone_set($location);
   echo PHP_EOL . str_pad($location, 24, ' ');
   echo date('r');
   $now = number_format(time());
   echo " $now";
}

Open in new window

0
 

Author Closing Comment

by:Lightwalker
ID: 34245972
Thank you Ray,

I am sorry for my confusion, and thank you so much for your help, I really really appreciate it.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34247420
Yeah - it's not completely intuitive, but once you see how the PHP functions work, it becomes easier and easier to get it right.  Thanks for the points! ~Ray
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 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 …

840 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