Skip to content

Instantly share code, notes, and snippets.

@scarstens
Created November 30, 2012 01:44
Show Gist options
  • Select an option

  • Save scarstens/4173204 to your computer and use it in GitHub Desktop.

Select an option

Save scarstens/4173204 to your computer and use it in GitHub Desktop.
Days and Dates Calculation Functions
//pass the function the MySQL standardized date and retreive a date relative to wordpress GMT and wordpress date and time display options
if(!function_exists('sm_display_date')) { function sm_display_date($mysqldate) {
$display_date = date_i18n( get_option('date_format').' '.get_option('time_format'),strtotime($mysqldate), get_option('gmt_offset') );
return $display_date;
}}
//pass the function a UNIX TIMESTAMP or "Properly Formated Date/Time" and retreive a date formated for MySQL database date field type
//optionally pass a number of days to return a time XX days before or after the date/time sent
if(!function_exists('sm_mysql_date')) { function sm_mysql_date($time, $days = 0) {
$seconds = 60*60*24*$days;
$unix_time = strtotime($time)+$seconds;
$mysqldate = date( 'Y-m-d H:i:s', $unix_time);
return $mysqldate;
}}
if(!function_exists('sm_seconds_to_days')) { function sm_seconds_to_days($seconds) {
return ($seconds / 24 / 60 / 60);
}}
if(!function_exists('sm_days_between_dates')) { function sm_days_between_dates($date1, $date2 = '', $precision = '1') {
if (empty($date2))
$date2 = current_time('mysql');
//setup the times based on string dates, if dates are not strings return false.
if ( is_string($date1) )
$date1 = strtotime($date1);
else
return false;
if ( is_string($date2) )
$date2 = strtotime($date2);
else
return false;
$days = round( sm_seconds_to_days($date1 - $date2), $precision );
return $days;
}}
// convert date
if(!function_exists('convert_date')) { function convert_date($thedate, $format='') {
if($format =='')
$format = "m/d/Y";
if($thedate != '')
return date($format, strtotime($thedate));
else
return $thedate;
}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment