Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- date('Y-m-d H:i:s', strtotime($Site->getUTCOffset() . ' hours', strtotime(date($utcDate))));
- <?php
- /*
- Example usage:
- $unixtime = TimeUtil::dateTimeToTimestamp('2009-04-01 15:36:13');
- echo TimeUtil::UTCToPST("M d, Y - H:i:s", $unixtime);
- */
- // You should move this to your regular init method
- date_default_timezone_set('UTC'); // make this match the server timezone
- class TimeUtil {
- public static function timestampToDateTime($timestamp) {
- return gmdate('Y-m-d H:i:s', $timestamp);
- }
- public static function dateTimeToTimestamp($dateTime) {
- // dateTimeToTimestamp expects MySQL format
- // If it gets a fully numeric value, we'll assume it's a timestamp
- // You can comment out this if block if you don't want this behavior
- if(is_numeric($dateTime)) {
- // You should probably log an error here
- return $dateTime;
- }
- $date = new DateTime($dateTime);
- $ret = $date->format('U');
- return ($ret < 0 ? 0 : $ret);
- }
- public static function UTCToPST($format, $time) {
- $dst = intval(date("I", $time));
- $tzOffset = intval(date('Z', time()));
- return date($format, $time + $tzOffset - 28800 + $dst * 3600);
- }
- }
- <?php
- $mysqlDate = '2009-04-01 15:36:13';
- $dateTime = new DateTime ($mysqlDate);
- $dateTime->setTimezone(new DateTimeZone('America/Los_Angeles'));
- ?>
- function convert_time_zone($date_time, $from_tz, $to_tz)
- {
- $time_object = new DateTime($date_time, new DateTimeZone($from_tz));
- $time_object->setTimezone(new DateTimeZone($to_tz));
- return $time_object->format('Y-m-d H:i:s');
- }
- mysql> set time_zone='America/New_York';
- ALTER TABLE mytable CHANGE COLUMN Created Created timestamp NULL DEFAULT 0;
- <?php
- function getNoteDateTimeZone($date = null, $from_dtz = 'US/Central', $to_dtz = null) {
- //$from_zt = 'US/Central'; // Time Zone = -06:00
- if (is_null($date) == FALSE && is_null($from_dtz) == FALSE && is_null($to_dtz) == FALSE) {
- // set TimeZone from
- $time_object = new DateTime($date, new DateTimeZone($from_dtz));
- $time_now_object = new DateTime("now", new DateTimeZone($from_dtz));
- // Change TimeZone
- $time_object->setTimezone(new DateTimeZone(trim($to_dtz)));
- $time_now_object->setTimezone(new DateTimeZone(trim($to_dtz)));
- // Is day = day in $time_now_object, $time_object..?
- if ($time_now_object->format('d') == $time_object->format('d')) {
- return $time_object->format('H:i:s');
- } else {
- return $time_object->format('Y-m-d H:i:s');
- }
- } else {
- return '';
- }
- }
- ?>
- <?php
- $date = '2008-06-02 20:32:46';
- $dtz = 'America/Argentina/Buenos_Aires';
- echo getNoteDateTimeZone($date, 'US/Central', $dtz); // Out = 2008-06-02 23:32:46
- ?>
- ADDTIME($utcDate,$Site->getUTCOffset())
- function convert_to_user_date($date, $userTimeZone = 'America/Los_Angeles', $serverTimeZone = 'UTC', $format = 'n/j/Y g:i A')
- {
- $dateTime = new DateTime ($date, new DateTimeZone($serverTimeZone));
- $dateTime->setTimezone(new DateTimeZone($userTimeZone));
- return $dateTime->format($format);
- }
- function convert_to_server_date($date, $userTimeZone = 'America/Los_Angeles', $serverTimeZone = 'UTC', $format = 'n/j/Y g:i A')
- {
- $dateTime = new DateTime ($date, new DateTimeZone($userTimeZone));
- $dateTime->setTimezone(new DateTimeZone($serverTimeZone));
- return $dateTime->format($format);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement