| 1 |
<?php |
| 2 |
/** |
| 3 |
* Date Functions |
| 4 |
* |
| 5 |
* @package GamiPress\Date_Functions |
| 6 |
* @author GamiPress <contact@gamipress.com>, Ruben Garcia <rubengcdev@gmail.com> |
| 7 |
* @since 1.7.1 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Helper function to format a given date or timestamp |
| 14 |
* |
| 15 |
* @since 1.6.9 |
| 16 |
* |
| 17 |
* @param string $format Date format |
| 18 |
* @param mixed $date |
| 19 |
* |
| 20 |
* @return string|false |
| 21 |
*/ |
| 22 |
function gamipress_date( $format, $date = '' ) { |
| 23 |
|
| 24 |
$date_formatted = false; |
| 25 |
|
| 26 |
if( strtotime( $date ) ) { |
| 27 |
// Ensure date given is correct |
| 28 |
$date_formatted = date( $format, strtotime( $date ) ); |
| 29 |
} else if( absint( $date ) > 0 ) { |
| 30 |
// Support for timestamp value |
| 31 |
$date_formatted = date( $format, absint( $date ) ); |
| 32 |
} |
| 33 |
|
| 34 |
return $date_formatted; |
| 35 |
|
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Get a specific period date range |
| 40 |
* |
| 41 |
* @see gamipress_get_time_periods() |
| 42 |
* |
| 43 |
* @since 1.6.9 |
| 44 |
* |
| 45 |
* @param string $period |
| 46 |
* |
| 47 |
* @return array |
| 48 |
*/ |
| 49 |
function gamipress_get_period_range( $period = '' ) { |
| 50 |
|
| 51 |
// Setup date range var |
| 52 |
$date_range = array( |
| 53 |
'start' => '', |
| 54 |
'end' => '', |
| 55 |
); |
| 56 |
|
| 57 |
if( $period !== '' ) { |
| 58 |
|
| 59 |
switch( $period ) { |
| 60 |
case 'today': |
| 61 |
$date_range = array( |
| 62 |
'start' => date( 'Y-m-d 00:00:00', current_time( 'timestamp' ) ), |
| 63 |
'end' => '', |
| 64 |
); |
| 65 |
break; |
| 66 |
case 'yesterday': |
| 67 |
$date_range = array( |
| 68 |
'start' => date( 'Y-m-d 00:00:00', strtotime( '-1 day', current_time( 'timestamp' ) ) ), |
| 69 |
'end' => date( 'Y-m-d 23:59:59', strtotime( '-1 day', current_time( 'timestamp' ) ) ), |
| 70 |
); |
| 71 |
break; |
| 72 |
case 'current-week': |
| 73 |
case 'this-week': |
| 74 |
$date_range = gamipress_get_date_range( 'week' ); |
| 75 |
break; |
| 76 |
case 'past-week': |
| 77 |
$previous_week = strtotime( '-1 week', current_time( 'timestamp' ) ); |
| 78 |
$date_range = gamipress_get_date_range( 'week', $previous_week ); |
| 79 |
break; |
| 80 |
case 'current-month': |
| 81 |
case 'this-month': |
| 82 |
$date_range = gamipress_get_date_range( 'month' ); |
| 83 |
break; |
| 84 |
case 'past-month': |
| 85 |
$previous_month = strtotime( '-1 month', current_time( 'timestamp' ) ); |
| 86 |
$date_range = gamipress_get_date_range( 'month', $previous_month ); |
| 87 |
break; |
| 88 |
case 'current-year': |
| 89 |
case 'this-year': |
| 90 |
$date_range = gamipress_get_date_range( 'year' ); |
| 91 |
break; |
| 92 |
case 'past-year': |
| 93 |
$previous_year = strtotime( '-1 year', current_time( 'timestamp' ) ); |
| 94 |
$date_range = gamipress_get_date_range( 'year', $previous_year ); |
| 95 |
break; |
| 96 |
default: |
| 97 |
// For custom ranges use 'gamipress_get_period_range' filter |
| 98 |
break; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Filter the period date range |
| 104 |
* |
| 105 |
* @since 1.6.9 |
| 106 |
* |
| 107 |
* @param array $date_range An array with period date range |
| 108 |
* @param string $period Given period, see gamipress_get_time_periods() |
| 109 |
*/ |
| 110 |
return $date_range = apply_filters( 'gamipress_get_period_range', $date_range, $period ); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Helper function to get a range date based on a given date |
| 115 |
* |
| 116 |
* @since 1.6.9 |
| 117 |
* |
| 118 |
* @param string $range (week|month|year) |
| 119 |
* @param integer|string $date |
| 120 |
* |
| 121 |
* @return array |
| 122 |
*/ |
| 123 |
function gamipress_get_date_range( $range = '', $date = 0 ) { |
| 124 |
|
| 125 |
if( gettype( $date ) === 'string' ) { |
| 126 |
$date = strtotime( $date ); |
| 127 |
} |
| 128 |
|
| 129 |
if( ! $date ) { |
| 130 |
$date = current_time( 'timestamp' ); |
| 131 |
} |
| 132 |
|
| 133 |
$start_date = 0; |
| 134 |
$end_date = 0; |
| 135 |
|
| 136 |
switch( $range ) { |
| 137 |
case 'week': |
| 138 |
|
| 139 |
// Weekly range |
| 140 |
$start_date = strtotime( 'monday this week', $date ); |
| 141 |
$end_date = strtotime( 'midnight', strtotime( 'sunday this week', $date ) ); |
| 142 |
|
| 143 |
break; |
| 144 |
case 'month': |
| 145 |
|
| 146 |
// Monthly range |
| 147 |
$start_date = strtotime( date( 'Y-m-01', $date ) ); |
| 148 |
$end_date = strtotime( 'midnight', strtotime( 'last day of this month', $date ) ); |
| 149 |
|
| 150 |
break; |
| 151 |
case 'year': |
| 152 |
|
| 153 |
// Yearly range |
| 154 |
$start_date = strtotime( date( 'Y-01-01', $date ) ); |
| 155 |
$end_date = strtotime( date( 'Y-12-31', $date ) ); |
| 156 |
|
| 157 |
break; |
| 158 |
} |
| 159 |
|
| 160 |
return array( |
| 161 |
'start' => date( 'Y-m-d 00:00:00', $start_date ), |
| 162 |
'end' => date( 'Y-m-d 23:59:59', $end_date ) |
| 163 |
); |
| 164 |
|
| 165 |
} |