date-tools.php
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Classes; |
| 5 | |
| 6 | // If this file is called directly, abort. |
| 7 | if ( ! defined( 'WPINC' ) ) { |
| 8 | die; |
| 9 | } |
| 10 | |
| 11 | class Date_Tools { |
| 12 | |
| 13 | const TIME = 1; |
| 14 | const DATE = 2; |
| 15 | const DATETIME = 3; |
| 16 | |
| 17 | public static function time_to_string( string $value, int $stamp_format = self::DATE ): string { |
| 18 | // Min/max come from the field block's own attributes (admin-authored). |
| 19 | $value = jet_fb_parse_dynamic_trusted( $value ); |
| 20 | |
| 21 | if ( ! is_scalar( $value ) ) { |
| 22 | return ''; |
| 23 | } |
| 24 | |
| 25 | if ( self::TIME === $stamp_format ) { |
| 26 | return $value; |
| 27 | } |
| 28 | |
| 29 | $format = ''; |
| 30 | |
| 31 | switch ( $stamp_format ) { |
| 32 | case self::DATE: |
| 33 | $format = 'Y-m-d'; |
| 34 | break; |
| 35 | case self::DATETIME: |
| 36 | $format = 'Y-m-d\TH:i'; |
| 37 | break; |
| 38 | } |
| 39 | |
| 40 | // phpcs:ignore WordPress.DateTime.RestrictedFunctions |
| 41 | return Tools::is_valid_timestamp( $value ) ? date( $format, $value ) : $value; |
| 42 | } |
| 43 | |
| 44 | } |
| 45 |