Plugin Directory

Changeset 1047427


Ignore:
Timestamp:
12/17/2014 07:24:51 PM (11 years ago)
Author:
freakingid
Message:

Put Date utilities into static class

Location:
content-scheduler/trunk
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • content-scheduler/trunk/.gitignore

    r706934 r1047427  
    1313sitemap.xml
    1414sitemap.xml.gz
     15.DS_Store
  • content-scheduler/trunk/content-scheduler.php

    r1047426 r1047427  
    2828}
    2929
    30 
     30require_once "includes/DateUtilities.php";
    3131
    3232
     
    409409            if( !empty( $timestamp ) ) {
    410410                // we need to convert that into human readable so we can put it into our field
    411                 $datestring = $this->getReadableDateFromTimestamp( $timestamp );
     411                $datestring = DateUtilities::getReadableDateFromTimestamp( $timestamp );
    412412            } else {
    413413                $datestring = '';
     
    416416            echo '<label for="cs-expire-date">' . __("Expiration date and hour", 'contentscheduler' ) . '</label><br />';
    417417            echo '<input type="text" id="cs-expire-date" name="_cs-expire-date" value="'.$datestring.'" size="25" />';
    418             echo ' Input date and time as: Year-Month-Day Hour:00:00 e.g., 2010-11-25 08:00:00<br />';
     418            echo '<br />Input date and time in any valid Date and Time format,<br />e.g., Year-Month-Day Hour:Min:Sec, 2010-11-25 08:00:00';
    419419        }
    420420
     
    537537                // c. turn that into a utc timestamp
    538538                // d. store it. into $date before saving
    539                 $date = $this->getTimestampFromReadableDate( $dateString, $offsetHours );
     539                $date = DateUtilities::getTimestampFromReadableDate( $dateString, $offsetHours );
    540540            }
    541541            // We probably need to store the date differently,
     
    784784                        if( !empty( $timestamp ) ) {
    785785                            // convert
    786                             $ed = $this->getReadableDateFromTimestamp( $timestamp );
     786                            $ed = DateUtilities::getReadableDateFromTimestamp( $timestamp );
    787787                        } else {
    788788                            $ed = "Date misunderstood";
     
    835835                return false;
    836836            } else {
    837                 $expirationdt = $this->getReadableDateFromTimestamp( $timestamp );
     837                $expirationdt = DateUtilities::getReadableDateFromTimestamp( $timestamp );
    838838            }
    839839
     
    911911        }
    912912
    913         // TODO: Pull these out into a static class so they can be used in multiple places
    914         /*
    915             unixTimestamp       timestamp NOT adjusted for WordPress local time
    916             return something date and time as one string following WP site formatting settings
    917         */
    918         function getReadableDateFromTimestamp( $unixTimestamp ) {
    919             // get datetime object from unix timestamp
    920             $datetime = new DateTime( "@$unixTimestamp", new DateTimeZone( 'UTC' ) );
    921             // set the timezone to the site timezone
    922             $datetime->setTimezone( new DateTimeZone( $this->wp_get_timezone_string() ) );
    923             // return the unix timestamp adjusted to reflect the site's timezone
    924             // return $timestamp + $datetime->getOffset();
    925             $localTimestamp = $unixTimestamp + $datetime->getOffset();
    926             $dateString = date_i18n( get_option( 'date_format' ), $localTimestamp );
    927             $timeString = date( get_option( 'time_format' ), $localTimestamp );
    928             // put together and return
    929             return $dateString . " " . $timeString;
    930         }
    931         /*
    932             dateSTring      readalbe date / time string from user input field
    933             offsetHours     hours to add / remove from dateString-generated DateTime
    934             return          unit timestamp in UTC time (i.e., not 'local' time)
    935         */
    936         function getTimestampFromReadableDate( $dateString, $offsetHours = 0 ) {
    937             // get datetime object from site timezone
    938             $datetime = new DateTime( $dateString, new DateTimeZone( $this->wp_get_timezone_string() ) );
    939             // add the offsetHours
    940             // $date->add(new DateInterval('P10D'));
    941             $datetime->add( new DateInterval( "PT".$offsetHours."H" ) );
    942             // get the unix timestamp (adjusted for the site's timezone already)
    943             $timestamp = $datetime->format( 'U' );
    944             return $timestamp;   
    945         }
    946         /**
    947          * Returns the timezone string for a site, even if it's set to a UTC offset
    948          *
    949          * Adapted from http://www.php.net/manual/en/function.timezone-name-from-abbr.php#89155
    950          *
    951          * @return string valid PHP timezone string
    952          */
    953         function wp_get_timezone_string() {
    954             // if site timezone string exists, return it
    955             if ( $timezone = get_option( 'timezone_string' ) )
    956                 return $timezone;
    957             // get UTC offset, if it isn't set then return UTC
    958             if ( 0 === ( $utc_offset = get_option( 'gmt_offset', 0 ) ) )
    959                 return 'UTC';
    960             // adjust UTC offset from hours to seconds
    961             $utc_offset *= 3600;
    962             // attempt to guess the timezone string from the UTC offset
    963             if ( $timezone = timezone_name_from_abbr( '', $utc_offset, 0 ) ) {
    964                 return $timezone;
    965             }
    966             // last try, guess timezone string manually
    967             $is_dst = date( 'I' );
    968             foreach ( timezone_abbreviations_list() as $abbr ) {
    969                 foreach ( $abbr as $city ) {
    970                     if ( $city['dst'] == $is_dst && $city['offset'] == $utc_offset )
    971                         return $city['timezone_id'];
    972                 }
    973             }
    974             // fallback to UTC
    975             return 'UTC';
    976         }
    977913} // end ContentScheduler Class
    978914} // End IF Class ContentScheduler
  • content-scheduler/trunk/includes/update-postmeta-expiration-values.php

    r1047426 r1047427  
    44    and turn them into unix timestamps
    55*/
     6require_once "DateUtilities.php";
     7
    68// find posts that need to take some expiration action
    79global $wpdb;
     
    1921    {
    2022        // do the date munging
    21         $unixTimestamp = getTimestampFromReadableDate( $cur_post->meta_value, 0 );
     23        $unixTimestamp = DateUtilities::getTimestampFromReadableDate( $cur_post->meta_value, 0 );
    2224        // update it
    2325        update_post_meta( $cur_post->post_id, '_cs-expire-date', $unixTimestamp, $cur_post->meta_value );
    2426    } // end foreach
    2527} // endif
    26 
    27 // TODO: Pull these out into a static class so they can be used in multiple places
    28 /*
    29     dateSTring      readalbe date / time string from user input field
    30     offsetHours     hours to add / remove from dateString-generated DateTime
    31     return          unit timestamp in UTC time (i.e., not 'local' time)
    32 */
    33 function getTimestampFromReadableDate( $dateString, $offsetHours = 0 ) {
    34     // get datetime object from site timezone
    35     $datetime = new DateTime( $dateString, new DateTimeZone( wp_get_timezone_string() ) );
    36     // add the offsetHours
    37     // $date->add(new DateInterval('P10D'));
    38     $datetime->add( new DateInterval( "PT".$offsetHours."H" ) );
    39     // get the unix timestamp (adjusted for the site's timezone already)
    40     $timestamp = $datetime->format( 'U' );
    41     return $timestamp;   
    42 }
    43 /**
    44  * Returns the timezone string for a site, even if it's set to a UTC offset
    45  *
    46  * Adapted from http://www.php.net/manual/en/function.timezone-name-from-abbr.php#89155
    47  *
    48  * @return string valid PHP timezone string
    49  */
    50 function wp_get_timezone_string() {
    51     // if site timezone string exists, return it
    52     if ( $timezone = get_option( 'timezone_string' ) )
    53         return $timezone;
    54     // get UTC offset, if it isn't set then return UTC
    55     if ( 0 === ( $utc_offset = get_option( 'gmt_offset', 0 ) ) )
    56         return 'UTC';
    57     // adjust UTC offset from hours to seconds
    58     $utc_offset *= 3600;
    59     // attempt to guess the timezone string from the UTC offset
    60     if ( $timezone = timezone_name_from_abbr( '', $utc_offset, 0 ) ) {
    61         return $timezone;
    62     }
    63     // last try, guess timezone string manually
    64     $is_dst = date( 'I' );
    65     foreach ( timezone_abbreviations_list() as $abbr ) {
    66         foreach ( $abbr as $city ) {
    67             if ( $city['dst'] == $is_dst && $city['offset'] == $utc_offset )
    68                 return $city['timezone_id'];
    69         }
    70     }
    71     // fallback to UTC
    72     return 'UTC';
    73 }
    7428?>
Note: See TracChangeset for help on using the changeset viewer.