public static function convertLocalTimeToTimestamp($time) { // time to convert (just an example) // $time = 'Tuesday, April 21, 2009 2:32:46 PM'; // set this to the time zone provided by the user // $tz = get_option('wpla_local_timezone'); $tz = get_option('timezone_string'); if (!$tz) { $tz = wc_timezone_string(); } // 'Europe/London' // create the DateTimeZone object for later $dtzone = new DateTimeZone($tz); // now create the DateTime object for this time and user time zone $dtime = new DateTime($time, $dtzone); // print the timestamp $timestamp = $dtime->format('U'); return $timestamp; }
/** * Format a unix timestamp or MySQL datetime into an RFC3339 datetime * * @since 2.1 * @param int|string $timestamp unix timestamp or MySQL datetime * @param bool $convert_to_utc * @return string RFC3339 datetime */ public function format_datetime($timestamp, $convert_to_utc = false) { if ($convert_to_utc) { $timezone = new DateTimeZone(wc_timezone_string()); } else { $timezone = new DateTimeZone('UTC'); } try { if (is_numeric($timestamp)) { $date = new DateTime("@{$timestamp}"); } else { $date = new DateTime($timestamp, $timezone); } // convert to UTC by adjusting the time based on the offset of the site's timezone if ($convert_to_utc) { $date->modify(-1 * $date->getOffset() . ' seconds'); } } catch (Exception $e) { $date = new DateTime('@0'); } return $date->format('Y-m-d\\TH:i:s\\Z'); }
/** * @deprecated */ function woocommerce_timezone_string() { return wc_timezone_string(); }
/** * Test wc_timezone_string(). * * @since 2.2 */ public function test_wc_timezone_string() { // test when timezone string exists update_option('timezone_string', 'America/New_York'); $this->assertEquals('America/New_York', wc_timezone_string()); // restore default update_option('timezone_string', ''); // test with missing UTC offset delete_option('gmt_offset'); $this->assertEquals('UTC', wc_timezone_string()); // test with manually set UTC offset update_option('gmt_offset', -4); $this->assertEquals('America/Halifax', wc_timezone_string()); // test with invalid offset update_option('gmt_offset', 99); $this->assertEquals('UTC', wc_timezone_string()); // restore default update_option('gmt_offset', '0'); }
/** * Formats order note * * @since 1.0 * @param string $to number SMS message was sent to * @param int $sent_timestamp integer timestamp for when message was sent * @param string $message SMS message sent * @param string $status order status * @param bool $error true if there was an error sending SMS, false otherwise * @return string HTML-formatted order note */ private function format_order_note($to, $sent_timestamp, $message, $status, $error) { try { // get datetime object from unix timestamp $datetime = new DateTime("@{$sent_timestamp}", new DateTimeZone('UTC')); // change timezone to site timezone $datetime->setTimezone(new DateTimeZone(wc_timezone_string())); // return datetime localized to site date/time settings $formatted_datetime = date_i18n(wc_date_format() . ' ' . wc_time_format(), $sent_timestamp + $datetime->getOffset()); } catch (Exception $e) { // log error and set datetime for SMS to 'N/A' wc_twilio_sms()->log($e->getMessage()); $formatted_datetime = __('N/A', WC_Twilio_SMS::TEXT_DOMAIN); } ob_start(); ?> <p><strong><?php _e('SMS Notification', WC_Twilio_SMS::TEXT_DOMAIN); ?> </strong></p> <p><strong><?php _e('To', WC_Twilio_SMS::TEXT_DOMAIN); ?> : </strong><?php echo esc_html($to); ?> </p> <p><strong><?php _e('Date Sent', WC_Twilio_SMS::TEXT_DOMAIN); ?> : </strong><?php echo esc_html($formatted_datetime); ?> </p> <p><strong><?php _e('Message', WC_Twilio_SMS::TEXT_DOMAIN); ?> : </strong><?php echo esc_html($message); ?> </p> <p><strong><?php _e('Status', WC_Twilio_SMS::TEXT_DOMAIN); ?> : <span style="<?php echo $error ? 'color: red;' : 'color: green;'; ?> "><?php echo esc_html($status); ?> </span></strong></p> <?php return ob_get_clean(); }
/** * Compatibility for the wc_timezone_string() function, which only * exists in 2.1+ * * @since 2.0 * @return string a valid PHP timezone string for the site */ public static function wc_timezone_string() { if (self::is_wc_version_gte_2_1()) { return wc_timezone_string(); } else { // if site timezone string exists, return it if ($timezone = get_option('timezone_string')) { return $timezone; } // get UTC offset, if it isn't set then return UTC if (0 === ($utc_offset = get_option('gmt_offset', 0))) { return 'UTC'; } // adjust UTC offset from hours to seconds $utc_offset *= 3600; // attempt to guess the timezone string from the UTC offset $timezone = timezone_name_from_abbr('', $utc_offset); // last try, guess timezone string manually if (false === $timezone) { $is_dst = date('I'); foreach (timezone_abbreviations_list() as $abbr) { foreach ($abbr as $city) { if ($city['dst'] == $is_dst && $city['offset'] == $utc_offset) { return $city['timezone_id']; } } } } // fallback to UTC return 'UTC'; } }