[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 817
  • Last Modified:

convert time to EST

I'm using one of godaddys servers and the time is 2 hours behind EST. How cna I convert the tome to EST? Here is my current code,

$ELASTDROP = date('M d, Y h:i A', strtotime($ELASTDROP));
0
JPERKS1985
Asked:
JPERKS1985
  • 3
  • 2
1 Solution
 
leakim971PluritechnicianCommented:
Hello JPERKS1985,

Try with putenv : http://php.net/manual/fr/function.putenv.php


putenv("TZ=US/Eastern");

Open in new window

0
 
leakim971PluritechnicianCommented:
0
 
Ray PaseurCommented:
You may want to consider this in a broader context.  I have a telemarketing application that must implement the federal laws about when it is OK to place a telephone call to a residence (8:00AM to 9:00PM).  This must work correctly across each time zone, and while my code is not perfect (some states span time zones) it seems to handle most of the issues correctly.  What I am posting here is untested because it was ripped out of my production scripts, but hopefully it will give you a template for translating server time into client time.  In my case the client lookup is done on the basis of the state, but there may be other signals (such as geolocation of an IP address) that could tell you what timezone the client uses.  My servers are in EDT, and my client machines are across the USA.

Best regards, ~Ray
// EASTERN PREVAILING TIME
date_default_timezone_set("America/New_York");

// OFFICIAL US TIMEZONES
$official_us_timezones = array(
    'America/Puerto_Rico' => 'AST',
    'America/New_York'    => 'EDT',
    'America/Chicago'     => 'CDT',
    'America/Boise'       => 'MDT',
    'America/Phoenix'     => 'MST',
    'America/Los_Angeles' => 'PDT',
    'America/Juneau'      => 'AKDT',
    'Pacific/Honolulu'    => 'HST',
    'Pacific/Guam'        => 'ChST',
    'Pacific/Samoa'       => 'SST',
    'Pacific/Wake'        => 'WAKT'
  );

// TIMEZONE OFFSETS FROM AMERICA / NEW YORK -- ADD THIS TO THE LOCAL TIME TO GET THE REMOTE TIME
$official_us_timezone_subtractions = array(
    'AST'  => ' +1 hour',
    'EDT'  => ' +0 hour',
    'CDT'  => ' -1 hour',
    'MDT'  => ' -2 hour',
    'MST'  => ' -3 hour',
    'PDT'  => ' -3 hour',
    'AKDT' => ' -4 hour',
    'HST'  => ' -6 hour'
  );

// TIMEZONE OFFSETS FROM AMERICA / NEW YORK -- ADD THIS TO THE REMOTE TIME TO GET THE LOCAL TIME
$official_us_timezone_additions = array(
    'AST'  => ' -1 hour',
    'EDT'  => ' +0 hour',
    'CDT'  => ' +1 hour',
    'MDT'  => ' +2 hour',
    'MST'  => ' +3 hour',
    'PDT'  => ' +3 hour',
    'AKDT' => ' +4 hour',
    'HST'  => ' +6 hour'
  );


// GET TIME ZONE BY STATE
// IMPRECISE FOR SPLIT STATES: http://www.timetemperature.com/tzus/time_zone_boundaries.shtml
function get_timezone_by_state($s)
{
    $z['WI'] = 'CDT';
    $z['IL'] = 'CDT';
    $z['AL'] = 'CDT';
    $z['MN'] = 'CDT'; 
    $z['IA'] = 'CDT';
    $z['MO'] = 'CDT';
    $z['AR'] = 'CDT';
    $z['MS'] = 'CDT';
    $z['LA'] = 'CDT';
    $z['ND'] = 'CDT'; // SPLIT MDT
    $z['SD'] = 'CDT'; // SPLIT MDT
    $z['KS'] = 'CDT'; // SPLIT MDT
    $z['OK'] = 'CDT';
    $z['TX'] = 'CDT';

    $z['NE'] = 'MDT'; // SPLIT CDT
    $z['MT'] = 'MDT';
    $z['WY'] = 'MDT'; 
    $z['CO'] = 'MDT';
    $z['NM'] = 'MDT';
    $z['ID'] = 'MDT'; // SPLIT PDT
    $z['UT'] = 'MDT';

    $z['AZ'] = 'MST';

    $z['WA'] = 'PDT';
    $z['OR'] = 'PDT';
    $z['NV'] = 'PDT';
    $z['CA'] = 'PDT';

    $z['ME'] = 'EDT';
    $z['VT'] = 'EDT';
    $z['NH'] = 'EDT';
    $z['MA'] = 'EDT';
    $z['RI'] = 'EDT';
    $z['CT'] = 'EDT';
    $z['NY'] = 'EDT';
    $z['PA'] = 'EDT';
    $z['NJ'] = 'EDT';
    $z['DE'] = 'EDT';
    $z['MD'] = 'EDT';
    $z['DC'] = 'EDT';
    $z['VA'] = 'EDT';
    $z['NC'] = 'EDT';
    $z['SC'] = 'EDT';
    $z['GA'] = 'EDT';
    $z['FL'] = 'EDT'; // SPLIT CDT
    $z['MI'] = 'EDT';
    $z['OH'] = 'EDT';
    $z['IN'] = 'EDT'; // SPLIT CDT
    $z['KY'] = 'EDT'; // SPLIT CDT
    $z['TN'] = 'EDT'; // SPLIT CDT
    $z['WV'] = 'EDT';

    $z['HI'] = 'HST';
    $z['AK'] = 'AKDT';

    $z['ZZ'] = 'PDT'; // TESTING SIGNAL IF STATE = ZZ

    if (!isset($z[$s])) return 'PDT';
    return $z[$s];
}



// A FUNCTION TO SEE IF IT IS OK TO CALL NOW IN THE STATE TIME ZONE
function safe_to_call_now($datething='NOW', $state='ZZ')
{
    global $official_us_timezone_subtractions;

    // ADJUST THIS BASED ON AGENT AND PROSPECT STATES / TIMEZONES ???
    date_default_timezone_set("America/New_York");

    // BOGUS TIME STRING?
    if (!$timestamp = strtotime($datething)) return FALSE;
    $datething = date('Y-m-d H:i:s', $timestamp);

    // SET THE DEFAULT FOR TIMEZONE OFFSETS
    $sub_hours = ' +0 hour';

    // GET THE TIMEZONE ASSOCIATED WITH THIS STATE
    $timezone  = get_timezone_by_state($state);
    $sub_hours = $official_us_timezone_subtractions[$timezone];

    // GET THE NEW REMOTE TIMESTAMP FOR OUR DB LOOKUP
    $timestamp = strtotime($datething . $sub_hours);
    $state_time = date('His', $timestamp);

    // IMPLEMENTS FEDERAL 'OK TO CALL' TELEMARKETING RULES FROM 8AM to 9PM
    if ( ($state_time > '080000') && ($state_time < '210000') ) return TRUE;
    return FALSE;
}

Open in new window

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!

 
shaziashaikhCommented:
<?php
$time_offset ="525"; // Change this to your time zone
$time_a = ($time_offset * 120);
$time = date("h:i:s",time() + $time_a);
echo 'Current time is : '.$time;
?>
0
 
Ray PaseurCommented:
@shaziashaikh: You might want to explain that a little more.  I ran the code and it told me it is 4:24.  Since I am at about noon in EST and my server is in CST, a more reasonable answer might have been noon or 11:00am.  I do not follow the logic.  Thanks, ~Ray
0
 
leakim971PluritechnicianCommented:
Thanks for your work cyberwebservice!
0

Featured Post

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!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now