Codxplore.com

Latest news:

Sahih al-Bukhari (সহীহ বুখারী) is a free Hadith application for android. This application is advertisement free. Download now https://play.google.com/store/apps/details?id=com.akramhossin.bukharisharif

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 : 1164

Subscribe Us


Follow Us