Find this useful? Enter your email to receive occasional updates for securing PHP code.

Signing you up...

Thank you for signing up!

PHP Decode

<?php /** * Manage date and time conversion from and to UTC and local time. * The appli..

Decoded Output download

<?php 
/** 
* Manage date and time conversion from and to UTC and local time. 
* The application expects the timezone set in index.php to be 
* local time and time from database be in UTC. 
* Date from user input MUST be local time 
* 
* 
* @author Methu Mwangi <[email protected]> 
*/ 
class TimeUtilities 
{ 
  const format = "d-M,Y h:i:s A"; 
  const sformat = "Y-m-d H:i:s.v"; 
  /** 
  * @var int|string 
  */ 
  private static $sessionTimeZone; 
 
  /** 
  * Convert local time to UTC time. 
  * @param String $datetime date time to convert to UTC 
  * @return String | null UTC date time in format Y-m-d H:i:s.v 
  */ 
  public static function toUTC($datetime) 
  { 
    try { 
      return self::convertToUTC($datetime, true)->format(self::sformat); 
    } catch (Exception $exc) { 
      return "Invalid Date"; 
    } 
  } 
 
  /** 
  * Convert UTC to local time time. 
  * @param String $datetime UTC date time 
  * @return String | null local date time in format Y-m-d h:i:s.v A GMT+/-HH:MM 
  */ 
  public static function fromUTC($datetime) 
  { 
    try { 
      $datetime = self::convertToLocal($datetime, true); 
      $offset = $datetime->format("P"); 
      return $datetime->format(self::format) . " GMT" . $offset; 
    } catch (Exception $exc) { 
      return "Invalid Date"; 
    } 
  } 
 
  private static function convertToUTC($date, $throw = false) 
  { 
    return self::convert($date, date_default_timezone_get(), "UTC", $throw); 
  } 
 
  private static function convertToLocal($date, $throw = false) 
  { 
    $timezone = self::getSessionTimeZone(); 
    return self::convert($date, "UTC", $timezone, $throw); 
  } 
 
  private static function convert($date, $from, $to, $throw = false) 
  { 
    try { 
      $datetime = new DateTime($date, new DateTimeZone($from)); 
      $timezone = new DateTimeZone($to); 
      $datetime->setTimezone($timezone); 
      return $datetime; 
    } catch (Exception $e) { 
      CoreUtils::flog2( 
        "ERROR", 
        "Error passing date: " . $e->getMessage(), 
        __CLASS__, 
        __FUNCTION__, 
        __LINE__ 
      ); 
      if ($throw) { 
        throw $e; 
      } 
      return "Invalid date provided"; 
    } 
  } 
 
  private static function getSessionTimeZone() 
  { 
    if (self::$sessionTimeZone !== null) { 
      return self::$sessionTimeZone; 
    } 
    $clientCodeTimeZones = [ 
      "ADMIN" => "Africa/Maputo", 
      "BBMZ" => "Africa/Maputo", 
    ]; 
 
    if (isset($_SESSION) && is_array($_SESSION)) { 
      foreach ($_SESSION as $key => $value) { 
        if ( 
          strpos($key, "applications") !== false && 
          isset($value["userData"]["clientCode"]) 
        ) { 
          self::$sessionTimeZone = self::getTimeZoneForClientCode( 
            $value["userData"]["clientCode"], 
            $clientCodeTimeZones 
          ); 
          CoreUtils::flog2( 
            "INFO", 
            $value["userData"]["clientCode"], 
            " uses : " . self::$sessionTimeZone, 
            __CLASS__, 
            __FUNCTION__, 
            __LINE__ 
          ); 
          break; 
        } 
      } 
    } 
    return self::$sessionTimeZone ?? date_default_timezone_get(); 
  } 
 
  public static function getTimeZoneForClientCode( 
    $clientCode, 
    $clientTimeZones 
  ) { 
    foreach ($clientTimeZones as $client => $timeZone) { 
      if ($client === $clientCode) { 
        return $timeZone; 
      } 
    } 
    return date_default_timezone_get(); 
  } 
} ?>

Did this file decode correctly?

Original Code

<?php
/**
* Manage date and time conversion from and to UTC and local time.
* The application expects the timezone set in index.php to be
* local time and time from database be in UTC.
* Date from user input MUST be local time
*
*
* @author Methu Mwangi <[email protected]>
*/
class TimeUtilities
{
  const format = "d-M,Y h:i:s A";
  const sformat = "Y-m-d H:i:s.v";
  /**
  * @var int|string
  */
  private static $sessionTimeZone;

  /**
  * Convert local time to UTC time.
  * @param String $datetime date time to convert to UTC
  * @return String | null UTC date time in format Y-m-d H:i:s.v
  */
  public static function toUTC($datetime)
  {
    try {
      return self::convertToUTC($datetime, true)->format(self::sformat);
    } catch (Exception $exc) {
      return "Invalid Date";
    }
  }

  /**
  * Convert UTC to local time time.
  * @param String $datetime UTC date time
  * @return String | null local date time in format Y-m-d h:i:s.v A GMT+/-HH:MM
  */
  public static function fromUTC($datetime)
  {
    try {
      $datetime = self::convertToLocal($datetime, true);
      $offset = $datetime->format("P");
      return $datetime->format(self::format) . " GMT" . $offset;
    } catch (Exception $exc) {
      return "Invalid Date";
    }
  }

  private static function convertToUTC($date, $throw = false)
  {
    return self::convert($date, date_default_timezone_get(), "UTC", $throw);
  }

  private static function convertToLocal($date, $throw = false)
  {
    $timezone = self::getSessionTimeZone();
    return self::convert($date, "UTC", $timezone, $throw);
  }

  private static function convert($date, $from, $to, $throw = false)
  {
    try {
      $datetime = new DateTime($date, new DateTimeZone($from));
      $timezone = new DateTimeZone($to);
      $datetime->setTimezone($timezone);
      return $datetime;
    } catch (Exception $e) {
      CoreUtils::flog2(
        "ERROR",
        "Error passing date: " . $e->getMessage(),
        __CLASS__,
        __FUNCTION__,
        __LINE__
      );
      if ($throw) {
        throw $e;
      }
      return "Invalid date provided";
    }
  }

  private static function getSessionTimeZone()
  {
    if (self::$sessionTimeZone !== null) {
      return self::$sessionTimeZone;
    }
    $clientCodeTimeZones = [
      "ADMIN" => "Africa/Maputo",
      "BBMZ" => "Africa/Maputo",
    ];

    if (isset($_SESSION) && is_array($_SESSION)) {
      foreach ($_SESSION as $key => $value) {
        if (
          strpos($key, "applications") !== false &&
          isset($value["userData"]["clientCode"])
        ) {
          self::$sessionTimeZone = self::getTimeZoneForClientCode(
            $value["userData"]["clientCode"],
            $clientCodeTimeZones
          );
          CoreUtils::flog2(
            "INFO",
            $value["userData"]["clientCode"],
            " uses : " . self::$sessionTimeZone,
            __CLASS__,
            __FUNCTION__,
            __LINE__
          );
          break;
        }
      }
    }
    return self::$sessionTimeZone ?? date_default_timezone_get();
  }

  public static function getTimeZoneForClientCode(
    $clientCode,
    $clientTimeZones
  ) {
    foreach ($clientTimeZones as $client => $timeZone) {
      if ($client === $clientCode) {
        return $timeZone;
      }
    }
    return date_default_timezone_get();
  }
}

Function Calls

None

Variables

None

Stats

MD5 f1818b2a8dc23d4054a55a3cd4efe49a
Eval Count 0
Decode Time 50 ms