Changeset 1047427
- Timestamp:
- 12/17/2014 07:24:51 PM (11 years ago)
- Location:
- content-scheduler/trunk
- Files:
-
- 1 added
- 3 edited
-
.gitignore (modified) (1 diff)
-
content-scheduler.php (modified) (7 diffs)
-
includes/DateUtilities.php (added)
-
includes/update-postmeta-expiration-values.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
content-scheduler/trunk/.gitignore
r706934 r1047427 13 13 sitemap.xml 14 14 sitemap.xml.gz 15 .DS_Store -
content-scheduler/trunk/content-scheduler.php
r1047426 r1047427 28 28 } 29 29 30 30 require_once "includes/DateUtilities.php"; 31 31 32 32 … … 409 409 if( !empty( $timestamp ) ) { 410 410 // 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 ); 412 412 } else { 413 413 $datestring = ''; … … 416 416 echo '<label for="cs-expire-date">' . __("Expiration date and hour", 'contentscheduler' ) . '</label><br />'; 417 417 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'; 419 419 } 420 420 … … 537 537 // c. turn that into a utc timestamp 538 538 // d. store it. into $date before saving 539 $date = $this->getTimestampFromReadableDate( $dateString, $offsetHours );539 $date = DateUtilities::getTimestampFromReadableDate( $dateString, $offsetHours ); 540 540 } 541 541 // We probably need to store the date differently, … … 784 784 if( !empty( $timestamp ) ) { 785 785 // convert 786 $ed = $this->getReadableDateFromTimestamp( $timestamp );786 $ed = DateUtilities::getReadableDateFromTimestamp( $timestamp ); 787 787 } else { 788 788 $ed = "Date misunderstood"; … … 835 835 return false; 836 836 } else { 837 $expirationdt = $this->getReadableDateFromTimestamp( $timestamp );837 $expirationdt = DateUtilities::getReadableDateFromTimestamp( $timestamp ); 838 838 } 839 839 … … 911 911 } 912 912 913 // TODO: Pull these out into a static class so they can be used in multiple places914 /*915 unixTimestamp timestamp NOT adjusted for WordPress local time916 return something date and time as one string following WP site formatting settings917 */918 function getReadableDateFromTimestamp( $unixTimestamp ) {919 // get datetime object from unix timestamp920 $datetime = new DateTime( "@$unixTimestamp", new DateTimeZone( 'UTC' ) );921 // set the timezone to the site timezone922 $datetime->setTimezone( new DateTimeZone( $this->wp_get_timezone_string() ) );923 // return the unix timestamp adjusted to reflect the site's timezone924 // 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 return929 return $dateString . " " . $timeString;930 }931 /*932 dateSTring readalbe date / time string from user input field933 offsetHours hours to add / remove from dateString-generated DateTime934 return unit timestamp in UTC time (i.e., not 'local' time)935 */936 function getTimestampFromReadableDate( $dateString, $offsetHours = 0 ) {937 // get datetime object from site timezone938 $datetime = new DateTime( $dateString, new DateTimeZone( $this->wp_get_timezone_string() ) );939 // add the offsetHours940 // $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 offset948 *949 * Adapted from http://www.php.net/manual/en/function.timezone-name-from-abbr.php#89155950 *951 * @return string valid PHP timezone string952 */953 function wp_get_timezone_string() {954 // if site timezone string exists, return it955 if ( $timezone = get_option( 'timezone_string' ) )956 return $timezone;957 // get UTC offset, if it isn't set then return UTC958 if ( 0 === ( $utc_offset = get_option( 'gmt_offset', 0 ) ) )959 return 'UTC';960 // adjust UTC offset from hours to seconds961 $utc_offset *= 3600;962 // attempt to guess the timezone string from the UTC offset963 if ( $timezone = timezone_name_from_abbr( '', $utc_offset, 0 ) ) {964 return $timezone;965 }966 // last try, guess timezone string manually967 $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 UTC975 return 'UTC';976 }977 913 } // end ContentScheduler Class 978 914 } // End IF Class ContentScheduler -
content-scheduler/trunk/includes/update-postmeta-expiration-values.php
r1047426 r1047427 4 4 and turn them into unix timestamps 5 5 */ 6 require_once "DateUtilities.php"; 7 6 8 // find posts that need to take some expiration action 7 9 global $wpdb; … … 19 21 { 20 22 // do the date munging 21 $unixTimestamp = getTimestampFromReadableDate( $cur_post->meta_value, 0 );23 $unixTimestamp = DateUtilities::getTimestampFromReadableDate( $cur_post->meta_value, 0 ); 22 24 // update it 23 25 update_post_meta( $cur_post->post_id, '_cs-expire-date', $unixTimestamp, $cur_post->meta_value ); 24 26 } // end foreach 25 27 } // endif 26 27 // TODO: Pull these out into a static class so they can be used in multiple places28 /*29 dateSTring readalbe date / time string from user input field30 offsetHours hours to add / remove from dateString-generated DateTime31 return unit timestamp in UTC time (i.e., not 'local' time)32 */33 function getTimestampFromReadableDate( $dateString, $offsetHours = 0 ) {34 // get datetime object from site timezone35 $datetime = new DateTime( $dateString, new DateTimeZone( wp_get_timezone_string() ) );36 // add the offsetHours37 // $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 offset45 *46 * Adapted from http://www.php.net/manual/en/function.timezone-name-from-abbr.php#8915547 *48 * @return string valid PHP timezone string49 */50 function wp_get_timezone_string() {51 // if site timezone string exists, return it52 if ( $timezone = get_option( 'timezone_string' ) )53 return $timezone;54 // get UTC offset, if it isn't set then return UTC55 if ( 0 === ( $utc_offset = get_option( 'gmt_offset', 0 ) ) )56 return 'UTC';57 // adjust UTC offset from hours to seconds58 $utc_offset *= 3600;59 // attempt to guess the timezone string from the UTC offset60 if ( $timezone = timezone_name_from_abbr( '', $utc_offset, 0 ) ) {61 return $timezone;62 }63 // last try, guess timezone string manually64 $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 UTC72 return 'UTC';73 }74 28 ?>
Note: See TracChangeset
for help on using the changeset viewer.