Skip to content

Commit e58bcda

Browse files
committed
- simplify resolvers for DatePicker and DateTimePicker
1 parent a9118a8 commit e58bcda

File tree

2 files changed

+7
-29
lines changed

2 files changed

+7
-29
lines changed

src/FieldType/DatePicker.php

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace WPGraphQL\Acf\FieldType;
33

4+
use DateTimeInterface;
45
use WPGraphQL\Acf\FieldConfig;
56

67
class DatePicker {
@@ -24,27 +25,13 @@ public static function register_field_type(): void {
2425
'resolve' => static function ( $root, $args, $context, $info, $field_type, FieldConfig $field_config ) {
2526
$value = $field_config->resolve_field( $root, $args, $context, $info );
2627

27-
if ( empty( $value ) ) {
28-
return null;
29-
}
30-
31-
$acf_field = $field_config->get_acf_field();
32-
33-
// Get the return format from the ACF Field
34-
$return_format = $acf_field['return_format'] ?? null;
35-
36-
if ( empty( $return_format ) ) {
37-
return $value;
38-
}
39-
40-
$date_time = \DateTime::createFromFormat( $return_format . '|', $value );
28+
$timestamp = strtotime( $value );
4129

42-
if ( empty( $date_time ) ) {
30+
if ( false === $timestamp ) {
4331
return null;
4432
}
4533

46-
// appending '|' to the format prevents the minutes and seconds from being determined from the current time
47-
return $date_time->format( \DateTimeInterface::RFC3339 );
34+
return gmdate( DateTimeInterface::RFC3339, $timestamp );
4835
},
4936
]
5037
);

src/FieldType/DateTimePicker.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,13 @@ public static function register_field_type(): void {
2929
return null;
3030
}
3131

32-
$acf_field = $field_config->get_acf_field();
32+
$timestamp = strtotime( $value );
3333

34-
// Get the return format from the ACF Field
35-
$return_format = $acf_field['return_format'] ?? null;
36-
37-
if ( empty( $return_format ) ) {
38-
return $value;
39-
}
40-
41-
$date_time = \DateTime::createFromFormat( $return_format, $value );
42-
43-
if ( empty( $date_time ) ) {
34+
if ( false === $timestamp ) {
4435
return null;
4536
}
4637

47-
return $date_time->format( DateTimeInterface::RFC3339 );
38+
return gmdate( DateTimeInterface::RFC3339, $timestamp );
4839
},
4940
]
5041
);

0 commit comments

Comments
 (0)