-1

I'm getting date time in below format:

2018-04-13 09:19:53 EDT  

this needs to be converted into PST or IST.

I want a function to convert given datetime into required datetime.

function convertdatetimes(datetime,current_timezone_abbrevation,required_timezone_abbrevation)  

ex convertdatetimes(2018-04-13 09:19:53,EDT,PDT)  

Please help.

  • possible duplicate https://stackoverflow.com/questions/3905193/convert-time-and-date-from-one-time-zone-to-another-in-php – ubuntux Jun 17 '18 at 10:05
  • @ubuntux your suggested post working based on Time zone name. I need timezone conversion by Time zone abbreviation. – user3049475 Jun 17 '18 at 10:09
  • And how, for example, should the code know that by `IST` you mean "India Standard Time", "Irish Standard Time" or "Israel Standard Time"? – Matt Johnson-Pint Jun 17 '18 at 21:58

1 Answers1

1

Try this:

function convertDateTimes($dateTime, $fromTz, $toTz, $format = 'Y-m-d H:i:s') {
    $fromTz = new DateTimeZone($fromTz);
    $dateTime = new DateTime($dateTime, $fromTz);
    $toTz = new DateTimeZone($toTz);
    $dateTime->setTimeZone($toTz);

    return $dateTime->format($format);
}

Sample usage:

$dateTime = '2018-04-13 09:19:53';
$fromTz = 'EDT';
$toTz = 'PDT';
echo "Input: $dateTime EDT <br>";
echo 'Output: ', convertDateTimes($dateTime, $fromTz, $toTz), " $toTz";

Output:

Input: 2018-04-13 09:19:53 EDT 
Output: 2018-04-13 06:19:53 PDT
ubuntux
  • 394
  • 3
  • 12