Skip to content

Commit

Permalink
Merge pull request #370 from lukecarbis/issue-353
Browse files Browse the repository at this point in the history
Issue #353: Date Ranges should honour timezone settings.
  • Loading branch information
frankiejarrett committed Mar 28, 2014
2 parents dc416a5 + 921f80b commit e74592f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 24 deletions.
1 change: 1 addition & 0 deletions includes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ public static function admin_enqueue_scripts( $hook ) {
'confirm_purge' => __( 'Are you sure you want to delete all Stream activity records from the database? This cannot be undone.', 'stream' ),
'confirm_uninstall' => __( 'Are you sure you want to uninstall and deactivate Stream? This will delete all Stream tables from the database and cannot be undone.', 'stream' ),
),
'gmt_offset' => get_option( 'gmt_offset' ),
'current_screen' => $hook,
'current_page' => isset( $_GET['paged'] ) ? esc_js( $_GET['paged'] ) : '1',
'current_order' => isset( $_GET['order'] ) ? esc_js( $_GET['order'] ) : 'desc',
Expand Down
61 changes: 38 additions & 23 deletions includes/date-interval.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,67 +31,82 @@ public function __construct() {
* @return mixed|void
*/
public function get_predefined_intervals() {
$timezone = get_option( 'timezone_string' );

if ( empty( $timezone ) ) {
$gmt_offset = (int) get_option( 'gmt_offset' );
$timezone = timezone_name_from_abbr( null, $gmt_offset * 3600, true );
if ( false === $timezone ) {
$timezone = timezone_name_from_abbr( null, $gmt_offset * 3600, false );
}
if ( false === $timezone ) {
$timezone = null;
}
}

return apply_filters(
'wp_stream_predefined_date_intervals',
array(
'today' => array(
'label' => esc_html__( 'Today', 'stream' ),
'start' => Carbon::today(),
'start' => Carbon::today( $timezone )->startOfDay(),
'end' => Carbon::today( $timezone )->startOfDay(),
),
'yesterday' => array(
'label' => esc_html__( 'Yesterday', 'stream' ),
'start' => Carbon::today()->subDay(),
'end' => Carbon::today()->subSecond(),
'start' => Carbon::today( $timezone )->startOfDay()->subDay(),
'end' => Carbon::today( $timezone )->startOfDay()->subSecond(),
),
'last-7-days' => array(
'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 7 ),
'start' => Carbon::today()->subDays( 7 ),
'end' => Carbon::today(),
'start' => Carbon::today( $timezone )->subDays( 7 ),
'end' => Carbon::today( $timezone ),
),
'last-14-days' => array(
'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 14 ),
'start' => Carbon::today()->subDays( 14 ),
'end' => Carbon::today(),
'start' => Carbon::today( $timezone )->subDays( 14 ),
'end' => Carbon::today( $timezone ),
),
'last-30-days' => array(
'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 30 ),
'start' => Carbon::today()->subDays( 30 ),
'end' => Carbon::today(),
'start' => Carbon::today( $timezone )->subDays( 30 ),
'end' => Carbon::today( $timezone ),
),
'this-month' => array(
'label' => esc_html__( 'This Month', 'stream' ),
'start' => Carbon::today()->day( 1 ),
'start' => Carbon::today( $timezone )->day( 1 ),
),
'last-month' => array(
'label' => esc_html__( 'Last Month', 'stream' ),
'start' => Carbon::today()->day( 1 )->subMonth(),
'end' => Carbon::today()->day( 1 )->subSecond(),
'start' => Carbon::today( $timezone )->day( 1 )->subMonth(),
'end' => Carbon::today( $timezone )->day( 1 )->subSecond(),
),
'last-3-months' => array(
'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 3 ),
'start' => Carbon::today()->subMonths( 3 ),
'end' => Carbon::today(),
'start' => Carbon::today( $timezone )->subMonths( 3 ),
'end' => Carbon::today( $timezone ),
),
'last-6-months' => array(
'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 6 ),
'start' => Carbon::today()->subMonths( 6 ),
'end' => Carbon::today(),
'start' => Carbon::today( $timezone )->subMonths( 6 ),
'end' => Carbon::today( $timezone ),
),
'last-12-months' => array(
'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 12 ),
'start' => Carbon::today()->subMonths( 12 ),
'end' => Carbon::today(),
'start' => Carbon::today( $timezone )->subMonths( 12 ),
'end' => Carbon::today( $timezone ),
),
'this-year' => array(
'label' => esc_html__( 'This Year', 'stream' ),
'start' => Carbon::today()->day( 1 )->month( 1 ),
'start' => Carbon::today( $timezone )->day( 1 )->month( 1 ),
),
'last-year' => array(
'label' => esc_html__( 'Last Year', 'stream' ),
'start' => Carbon::today()->day( 1 )->month( 1 )->subYear(),
'end' => Carbon::today()->day( 1 )->month( 1 )->subSecond(),
)
)
'start' => Carbon::today( $timezone )->day( 1 )->month( 1 )->subYear(),
'end' => Carbon::today( $timezone )->day( 1 )->month( 1 )->subSecond(),
),
),
$timezone
);
}

Expand Down
21 changes: 20 additions & 1 deletion ui/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,9 +440,28 @@ jQuery(function($){

if ( jQuery.datepicker ) {

// Apply a GMT offset due to Date() using the visitor's local time
var siteGMTOffsetHours = parseFloat( wp_stream.gmt_offset );
var localGMTOffsetHours = new Date().getTimezoneOffset() / 60 * -1;
var totalGMTOffsetHours = siteGMTOffsetHours - localGMTOffsetHours;

var localTime = new Date();
var siteTime = new Date( localTime.getTime() + ( totalGMTOffsetHours * 60 * 60 * 1000 ) );
var dayOffset = '0';

// check if the site date is different from the local date, and set a day offset
if ( localTime.getDate() !== siteTime.getDate() || localTime.getMonth() !== siteTime.getMonth() ) {
if ( localTime.getTime() < siteTime.getTime() ) {
dayOffset = '+1d';
} else {
dayOffset = '-1d';
}
}

datepickers.datepicker({
dateFormat: 'yy/mm/dd',
maxDate: 0,
maxDate: dayOffset,
defaultDate: siteTime,
beforeShow: function() {
$(this).prop( 'disabled', true );
},
Expand Down

0 comments on commit e74592f

Please sign in to comment.