activities_helper.php
3 months ago
agent_helper.php
3 months ago
analytics_helper.php
1 month ago
auth_helper.php
2 months ago
blocks_helper.php
3 months ago
booking_helper.php
2 months ago
bricks_helper.php
3 months ago
bundles_helper.php
3 months ago
calendar_helper.php
3 months ago
carts_helper.php
3 months ago
connector_helper.php
3 months ago
csv_helper.php
3 months ago
customer_helper.php
1 month ago
customer_import_helper.php
3 months ago
database_helper.php
3 months ago
debug_helper.php
3 months ago
defaults_helper.php
3 months ago
elementor_helper.php
3 months ago
email_helper.php
3 months ago
encrypt_helper.php
3 months ago
events_helper.php
3 months ago
form_helper.php
3 months ago
icalendar_helper.php
3 months ago
image_helper.php
3 months ago
invoices_helper.php
2 months ago
license_helper.php
3 months ago
location_helper.php
3 months ago
marketing_systems_helper.php
3 months ago
meeting_systems_helper.php
3 months ago
menu_helper.php
2 months ago
meta_helper.php
3 months ago
migrations_helper.php
3 months ago
money_helper.php
3 months ago
notifications_helper.php
3 months ago
nps_survey_helper.php
3 months ago
order_intent_helper.php
1 month ago
orders_helper.php
3 months ago
otp_helper.php
3 months ago
pages_helper.php
3 months ago
params_helper.php
3 months ago
payments_helper.php
2 months ago
plugin_version_update_helper.php
2 months ago
price_breakdown_helper.php
3 months ago
process_jobs_helper.php
3 months ago
processes_helper.php
3 months ago
replacer_helper.php
3 months ago
resource_helper.php
3 months ago
roles_helper.php
3 months ago
router_helper.php
3 months ago
service_helper.php
3 months ago
sessions_helper.php
3 months ago
settings_helper.php
2 months ago
short_links_systems_helper.php
3 months ago
shortcodes_helper.php
2 months ago
sms_helper.php
3 months ago
steps_helper.php
1 month ago
stripe_connect_helper.php
2 months ago
styles_helper.php
3 months ago
support_topics_helper.php
3 months ago
time_helper.php
2 months ago
timeline_helper.php
2 months ago
transaction_helper.php
3 months ago
transaction_intent_helper.php
3 months ago
util_helper.php
3 months ago
version_specific_updates_helper.php
3 months ago
whatsapp_helper.php
3 months ago
work_periods_helper.php
3 months ago
wp_datetime.php
3 months ago
wp_user_helper.php
3 months ago
wp_datetime.php
85 lines
| 1 | <?php |
| 2 | class OsWpDateTime extends DateTime { |
| 3 | function __construct( ?string $time = 'now', ?DateTimeZone $timezone = null ) { |
| 4 | $timezone = ( $timezone instanceof DateTimeZone ) ? $timezone : OsTimeHelper::get_wp_timezone(); |
| 5 | try { |
| 6 | if ( empty( $time ) ) { |
| 7 | $time = 'now'; |
| 8 | } |
| 9 | parent::__construct( $time, $timezone ); |
| 10 | } catch ( Exception $e ) { |
| 11 | OsDebugHelper::log( 'Error parsing date: ' . $e->getMessage(), 'date_parsing' ); |
| 12 | return parent::__construct( 'now', $timezone ); |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | public static function datetime_in_utc( DateTime $datetime, $format = false ) { |
| 17 | $utc_datetime = clone $datetime; |
| 18 | $utc_datetime->setTimezone( new DateTimeZone( 'UTC' ) ); |
| 19 | return $format ? $utc_datetime->format( $format ) : $utc_datetime; |
| 20 | } |
| 21 | |
| 22 | public static function date_to_db_format( $date_string, $default = '' ) { |
| 23 | if ( empty( $date_string ) ) { |
| 24 | return $default; |
| 25 | } |
| 26 | try { |
| 27 | $date = self::os_createFromFormat( OsSettingsHelper::get_date_format(), $date_string ); |
| 28 | return $date->format( 'Y-m-d' ); |
| 29 | } catch ( Exception $e ) { |
| 30 | return $default; |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | public static function date_from_db_format( $date_string, $default = '' ) { |
| 35 | if ( empty( $date_string ) ) { |
| 36 | return $default; |
| 37 | } |
| 38 | try { |
| 39 | $timezone = new DateTimeZone( 'UTC' ); |
| 40 | $date = self::os_createFromFormat( 'Y-m-d', $date_string, $timezone ); |
| 41 | return $date->format( OsSettingsHelper::get_date_format() ); |
| 42 | } catch ( Exception $e ) { |
| 43 | return $default; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | public static function os_createFromFormat( $format, $datetime_string, $timezone = false ) { |
| 48 | $timezone = ( $timezone ) ? $timezone : OsTimeHelper::get_wp_timezone(); |
| 49 | return self::createFromFormat( $format, $datetime_string, $timezone ); |
| 50 | } |
| 51 | |
| 52 | // TODO will be deprecated, moved to GCal addon |
| 53 | public static function os_get_start_of_google_event( $google_event ) { |
| 54 | if ( ! empty( $google_event->start->dateTime ) ) { |
| 55 | $date_string = $google_event->start->dateTime; |
| 56 | $date_format = \DateTime::RFC3339; |
| 57 | $timezone = new DateTimeZone( $google_event->start->timeZone ); |
| 58 | } else { |
| 59 | // Full day event |
| 60 | $date_string = $google_event->start->date . ' 00:00:00'; |
| 61 | $date_format = LATEPOINT_DATETIME_DB_FORMAT; |
| 62 | $timezone = false; |
| 63 | } |
| 64 | return self::os_createFromFormat( $date_format, $date_string, $timezone ); |
| 65 | } |
| 66 | |
| 67 | // TODO will be deprecated, moved to GCal addon |
| 68 | public static function os_get_end_of_google_event( $google_event ) { |
| 69 | if ( ! empty( $google_event->end->dateTime ) ) { |
| 70 | $date_string = $google_event->end->dateTime; |
| 71 | $date_format = \DateTime::RFC3339; |
| 72 | return self::os_createFromFormat( $date_format, $date_string ); |
| 73 | } else { |
| 74 | // Full day event |
| 75 | // !important, in full day events of Google Calendar - start day is inclusive and the end day is exclusive https://stackoverflow.com/questions/34992747/google-calendar-json-api-full-day-events-always-one-day-longer |
| 76 | $date_string = $google_event->end->date . ' 23:59:59'; |
| 77 | $date_format = LATEPOINT_DATETIME_DB_FORMAT; |
| 78 | $temp_date = self::os_createFromFormat( $date_format, $date_string ); |
| 79 | // move back 1 day to accomodate Google rule that end date is 1 day ahead of actual end date of a full day event |
| 80 | $temp_date->modify( '-1 day' ); |
| 81 | return $temp_date; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 |