Skip to content

Instantly share code, notes, and snippets.

@geilt
Last active June 12, 2017 06:00
Show Gist options
  • Save geilt/7206006d827c6f916268e07536d938d4 to your computer and use it in GitHub Desktop.
Save geilt/7206006d827c6f916268e07536d938d4 to your computer and use it in GitHub Desktop.
Convert Timezones in and out of System in a simple way!
//https://gist.github.com/NTICompass/9375143
/**
* Gets Date based on PHP formats into Moment Format.
* @param {[type]} m [description]
* @return {[type]} [description]
*/
(function(m){
/*
* PHP => moment.js
*
* http://www.php.net/manual/en/function.date.php
* http://momentjs.com/docs/#/displaying/format/
*/
var formatMap = {
d: 'DD',
D: 'ddd',
j: 'D',
l: 'dddd',
N: 'E',
S: function(){
return '['+this.format('Do').replace(/\d*/g, '')+']';
},
w: 'd',
z: function(){
return this.format('DDD') - 1;
},
W: 'W',
F: 'MMMM',
m: 'MM',
M: 'MMM',
n: 'M',
t: function(){
return this.daysInMonth();
},
L: function(){
return this.isLeapYear() ? 1 : 0;
},
o: 'GGGG',
Y: 'YYYY',
y: 'YY',
a: 'a',
A: 'A',
B: function(){
var thisUTC = this.clone().utc(),
// Shamelessly stolen from http://javascript.about.com/library/blswatch.htm
swatch = ((thisUTC.hours()+1) % 24) + (thisUTC.minutes() / 60) + (thisUTC.seconds() / 3600);
return Math.floor(swatch * 1000 / 24);
},
g: 'h',
G: 'H',
h: 'hh',
H: 'HH',
i: 'mm',
s: 'ss',
u: '[u]', // not sure if moment has this
e: '[e]', // moment does not have this
I: function(){
return this.isDST() ? 1 : 0;
},
O: 'ZZ',
P: 'Z',
T: '[T]', // deprecated in moment
Z: function(){
return parseInt(this.format('ZZ'), 10) * 36;
},
c: 'YYYY-MM-DD[T]HH:mm:ssZ',
r: 'ddd, DD MMM YYYY HH:mm:ss ZZ',
U: 'X'
},
formatEx = /[dDjlNSwzWFmMntLoYyaABgGhHisueIOPTZcrU]/g;
m.fn.formatPHP = function(format){
var that = this;
return this.format(format.replace(formatEx, function(phpStr){
return typeof formatMap[phpStr] === 'function' ? formatMap[phpStr].call(that) : formatMap[phpStr];
}));
};
m.convertPHPtoMoment = function(format){
var that = this;
return format.replace(formatEx, function(phpStr){
return typeof formatMap[phpStr] === 'function' ? formatMap[phpStr].call(that) : formatMap[phpStr];
});
};
}(moment));
/**
* Set Formats of Dates in Moment from PHP. Set the format variables to your objects output in JS from PHP.
* @param {[type]} typeof moment ! [description]
* @return {[type]} [description]
*/
var formats = {
php: {
date: 'Y-m-d',
time: 'H:i:s',
datetime: 'Y-m-d H:i:s'
};
if(typeof moment !== 'undefined'){
formats.moment = {
date: moment.convertPHPtoMoment(formats.php.date),
time: moment.convertPHPtoMoment(formats.php.time),
datetime: moment.convertPHPtoMoment(formats.php.datetime)
};
} else {
console.log(`Couldn't get moment formats since moment isn't loaded`);
}
<?php
/**
* Missing from this file are the actual formats and user meta. You would need to write these for your system yourself.
* Accepts PHP / MYQL Timezones. Best to use zones such as US/Eastern - US/Pacific, UTC, etc.
*/
/**
* Account Time and Date Formats.
* @return [type] [description]
*/
function get_account_timezone(){
return !empty(get_account_options()->formats->time_zone) ? get_account_options()->formats->time_zone : TIMEZONE;
}
function get_account_date_format(){
return !empty(get_account_options()->formats->date) ? get_account_options()->formats->date : FORMAT_DATE;
}
function get_account_time_format(){
return !empty(get_account_options()->formats->time) ? get_account_options()->formats->time : FORMAT_TIME;
}
function get_account_datetime_format(){
return !empty(get_account_options()->formats->datetime) ? get_account_options()->formats->datetime : FORMAT_DATETIME;
}
/**
* User Timezone and Date Formats
* @return [type] [description]
*/
function get_user_timezone(){
return !empty(get_user_meta(get_account_id(), get_user_id(), 'format_timezone')) ? get_user_meta(get_account_id(), get_user_id(), 'format_timezone') : get_account_timezone();
}
function get_user_date_format(){
return !empty(get_user_meta(get_account_id(), get_user_id(), 'format_date')) ? get_user_meta(get_account_id(), get_user_id(), 'format_date') : get_account_date_format();
}
function get_user_time_format(){
return !empty(get_user_meta(get_account_id(), get_user_id(), 'format_time')) ? get_user_meta(get_account_id(), get_user_id(), 'format_time') : get_account_time_format();
}
function get_user_datetime_format(){
return !empty(get_user_meta(get_account_id(), get_user_id(), 'format_datetime')) ? get_user_meta(get_account_id(), get_user_id(), 'format_datetime') : get_account_datetime_format();
}
/**
* Current Local Dates and Times Formatted
* @return [type] [description]
*/
function get_local_datetime(){
return to_local_timezone(date('Y-m-d H:i:s'), 'datetime');
}
function get_local_date(){
return to_local_timezone(date('Y-m-d H:i:s'), 'date');
}
function get_local_time(){
return to_local_timezone(date('H:i:s'), 'time');
}
/** Current System Dates and Times Formatted
* @return [type] [description]
*/
function get_system_datetime(){
return to_system_timezone(date('Y-m-d H:i:s'), 'datetime');
}
function get_system_date(){
return to_system_timezone(date('Y-m-d H:i:s'), 'date');
}
function get_system_time(){
return to_system_timezone(date('H:i:s'), 'time');
}
/**
* Formats date with no timezone conversion but checks user settings.
* @param [type] $date [description]
* @return [type] [description]
*/
function to_user_date_format($date){
$date = new DateTime($date);
return $date->format(get_user_date_format());
}
function to_user_time_format($date){
$date = new DateTime($date);
return $date->format(get_user_time_format());
}
function to_user_datetime_format($date){
$date = new DateTime($date);
return $date->format(get_user_datetime_format());
}
/**
* Converts System Dates to Local Dates.
* @param [type] $date [description]
* @param string $type [description]
* @return [type] [description]
*/
function to_local_timezone($date, $type = 'datetime'){
$system_timezone = new DateTimeZone(TIMEZONE);
$date = new DateTime($date, $system_timezone);
if(TIMEZONE !== get_user_timezone()){
$user_timezone = new DateTimeZone(get_user_timezone());
$date->setTimeZone($user_timezone);
}
if($type == 'datetime') return $date->format(get_user_datetime_format());
if($type == 'date') return $date->format(get_user_date_format());
if($type == 'time') return $date->format(get_user_time_format());
return $date->format(get_user_datetime_format());
}
/**
* Converts Local Dates to System Dates. SYSTEM EXPECTS SERVER AND DATABASE TO BE SET TO SAME TIME ZONE!!!
* @return [type] [description]
*/
function to_system_timezone($date, $type = ''){
$system_timezone = new DateTimeZone(TIMEZONE);
$user_timezone = new DateTimeZone(get_user_timezone());
$date = new DateTime($date, $user_timezone);
$date->setTimeZone($system_timezone);
if($type == 'datetime') return $date->format('Y-m-d H:i:s');
if($type == 'date') return $date->format('Y-m-d');
if($type == 'time') return $date->format('H:i:s');
return $date->format('Y-m-d H:i:s');
}
/**
* Converts Database Dates to Local Dates. SYSTEM EXPECTS SERVER AND DATABASE TO BE SET TO SAME TIME ZONE!!!
* @return [type] [description]
*/
function to_local_timezone_sql($datetime, $name = ''){
if(TIMEZONE !== get_user_timezone()){
$system_timezone = strtoupper(TIMEZONE);
$user_timezone = strtoupper(get_user_timezone());
return "CONVERT_TZ( " . $datetime . ", '" . $system_timezone . "', '" . $user_timezone . "' )" . ( !empty($name) ? " as '" . $name . "'" : '');
}
return $datetime . ( !empty($name) ? " as '" . $name . "'" : '');
}
/**
* Converts Local Dates to System Database Dates. SYSTEM EXPECTS SERVER AND DATABASE TO BE SET TO SAME TIME ZONE!!!
* @return [type] [description]
*/
function to_system_timezone_sql($datetime, $name = ''){
if(TIMEZONE !== get_user_timezone()){
$system_timezone = strtoupper(TIMEZONE);
$user_timezone = strtoupper(get_user_timezone());
return "CONVERT_TZ( " . $datetime . ", '" . $user_timezone . "', '" . $system_timezone . "' )" . ( !empty($name) ? " as '" . $name . "'" : '');
}
return $datetime . ( !empty($name) ? " as '" . $name . "'" : '');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment