Changeset 739 for trunk/lib


Ignore:
Timestamp:
Nov 24, 2020 4:03:27 AM (3 years ago)
Author:
anonymous
Message:

Add methods to support timezone conversion

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/App.inc.php

    r738 r739  
    116116        // Human-readable format used to display dates.
    117117        'date_format' => 'd M Y',
    118         'time_format' => 'h:i:s A',
     118        'time_format' => 'h:i A',
    119119        'lc_date_format' => '%e %b %Y', // Localized date for strftime() https://www.php.net/manual/en/function.strftime.php
    120120        'lc_time_format' => '%k:%M', // Localized time for strftime() https://www.php.net/manual/en/function.strftime.php
     
    18391839
    18401840    /*
     1841    * Create a DateTime object from a string and convert its timezone.
     1842    *
     1843    * @access   public
     1844    * @param    string  $datetime   A date-time string or unit timestamp, e.g., `now + 60 days` or `1606165903`.
     1845    * @param    string  $from_tz    A PHP timezone, e.g., UTC
     1846    * @param    string  $to_tz      A PHP timezone, e.g., America/Mexico_City
     1847    * @return   DateTime            A DateTime object ready to use with, e.g., ->format(
).
     1848    * @author   Quinn Comendant <quinn@strangecode.com>
     1849    * @since    23 Nov 2020 15:08:45
     1850    */
     1851    function convertTZ($datetime, $from_tz, $to_tz)
     1852    {
     1853        if (preg_match('/^\d+$/', $datetime)) {
     1854            // It's a timestamp, format as required by DateTime::__construct().
     1855            $datetime = "@$datetime";
     1856        }
     1857
     1858        $dt = new DateTime($datetime, new DateTimeZone($from_tz));
     1859        $dt->setTimezone(new DateTimeZone($to_tz));
     1860
     1861        return $dt;
     1862    }
     1863
     1864    /*
     1865    * Convert a given date-time string from php_timezone to user_timezone, and return formatted.
     1866    *
     1867    * @access   public
     1868    * @param    string  $datetime   A date-time string or unit timestamp, e.g., `now + 60 days` or `1606165903`.
     1869    * @param    string  $format     A date format string for DateTime->format(
) or strftime(
). Set to lc_date_format by default.
     1870    * @return   string              A formatted date in the user's timezone.
     1871    * @author   Quinn Comendant <quinn@strangecode.com>
     1872    * @since    23 Nov 2020 15:13:26
     1873    */
     1874    function dateToUserTZ($datetime, $format=null)
     1875    {
     1876        if (empty($datetime) || in_array($datetime, ['0000-00-00 00:00:00', '0000-00-00', '1000-01-01 00:00:00', '1000-01-01'])) {
     1877            // Zero dates in MySQL should never be displayed.
     1878            return '';
     1879        }
     1880
     1881        // Create a DateTime object and conver the timezone from server to user.
     1882        $dt = $this->convertTZ($datetime, $this->getParam('php_timezone'), $this->getParam('user_timezone'));
     1883
     1884        // By default, we try to use a localized date format. Set lc_date_format to null to use regular date_format instead.
     1885        $format = $format ?: $this->getParam('lc_date_format');
     1886        if ($format && mb_strpos($format, '%') !== false) {
     1887            // The data format is localized for strftime(). It only accepts a timestamp, which are always in UTC, so we hack this by offering the date from the user's timezone in a format without a TZ specified, which is used to a make a timestamp for strftime (we can't use DaateTime->format('U') because that would convert the date back to UTC).
     1888            return strftime($format, strtotime($dt->format('Y-m-d H:i:s')));
     1889        } else {
     1890            // Otherwise use a regular date format.
     1891            $format = $format ?: $this->getParam('date_format');
     1892            return $dt->format($format);
     1893        }
     1894    }
     1895
     1896    /*
     1897    * Convert a given date-time string from user_timezone to php_timezone, and formatted as YYYY-MM-DD HH:MM:SS.
     1898    *
     1899    * @access   public
     1900    * @param    string  $datetime   A date-time string or unit timestamp, e.g., `now + 60 days` or `1606165903`.
     1901    * @param    string  $format     A date format string for DateTime->format(
). Set to 'Y-m-d H:i:s' by default.
     1902    * @return   string              A formatted date in the server's timezone.
     1903    * @author   Quinn Comendant <quinn@strangecode.com>
     1904    * @since    23 Nov 2020 15:13:26
     1905    */
     1906    function dateToServerTZ($datetime, $format='Y-m-d H:i:s')
     1907    {
     1908        return $this->convertTZ($datetime, $this->getParam('user_timezone'), $this->getParam('php_timezone'))->format($format);
     1909    }
     1910
     1911    /*
    18411912    *
    18421913    *
Note: See TracChangeset for help on using the changeset viewer.