Codxplore.com

Latest news:

Quran ul karim is a free Quran application for android. This application is advertisement free application for android smartphone. Download from https://play.google.com/store/apps/details?id=com.akramhossain.quranulkarim&hl=en

PHP convert datetime to selected timezone


PHP convert datetime to selected timezone

PHP DateTime class provides all the date and time processing functionality.

The DateTimeZone class provides DateTime objects with all the required time zone information.

DateTimeZone require the timezone name as string format and it will return an object representing datetime of that particular time zone.

We need to follow these steps to convert any datetime to particular time zone:

  • Create a DateTimeZone object using the timezone
  • Create a DateTime object, providing it the time in the selected timezone
  • Convert the DateTime object to the timezone created in step 1
  • Output the time from the DateTime object


function getSiteTime($timestamp="") {
    $dtzone = new DateTimeZone('Europe/Paris');
    $time = date('r', $timestamp);
    $dtime = new DateTime($time);
    $dtime->setTimeZone($dtzone);
    $time = $dtime->format('m-d-Y, a\t g.i A');
    return $time;
}


To get the time call the function like  echo getSiteTime(time())

Tags : PHP,

Views : 740

Subscribe Us


Follow Us