Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 401 and 402]

   1  <?php
   2  
   3  namespace PhpXmlRpc\Helper;
   4  
   5  use PhpXmlRpc\PhpXmlRpc;
   6  
   7  /**
   8   * Helps to convert timestamps to the xml-rpc date format.
   9   *
  10   * Feature creep -- add support for custom TZs
  11   */
  12  class Date
  13  {
  14      /**
  15       * Given a timestamp, return the corresponding ISO8601 encoded string.
  16       *
  17       * Really, timezones ought to be supported but the XML-RPC spec says:
  18       *
  19       * "Don't assume a timezone. It should be specified by the server in its documentation what assumptions it makes
  20       *  about timezones."
  21       *
  22       * This routine always encodes to local time unless $utc is set to 1, in which case UTC output is produced and an
  23       * adjustment for the local timezone's offset is made
  24       *
  25       * @param int|\DateTimeInterface $timet timestamp or datetime
  26       * @param bool|int $utc (0 or 1)
  27       * @return string
  28       */
  29      public static function iso8601Encode($timet, $utc = 0)
  30      {
  31          if (is_a($timet, 'DateTimeInterface') || is_a($timet, 'DateTime')) {
  32              $timet = $timet->getTimestamp();
  33          }
  34          if (!$utc) {
  35              $t = date('Ymd\TH:i:s', $timet);
  36          } else {
  37              $t = gmdate('Ymd\TH:i:s', $timet);
  38          }
  39  
  40          return $t;
  41      }
  42  
  43      /**
  44       * Given an ISO8601 date string, return a timestamp in the localtime, or UTC.
  45       *
  46       * @param string $idate
  47       * @param bool|int $utc either 0 (assume date is in local time) or 1 (assume date is in UTC)
  48       *
  49       * @return int (timestamp) 0 if the source string does not match the xml-rpc dateTime format
  50       */
  51      public static function iso8601Decode($idate, $utc = 0)
  52      {
  53          $t = 0;
  54          if (preg_match(PhpXmlRpc::$xmlrpc_datetime_format, $idate, $regs)) {
  55              if ($utc) {
  56                  $t = gmmktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]);
  57              } else {
  58                  $t = mktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]);
  59              }
  60          }
  61  
  62          return $t;
  63      }
  64  }