Date Format Mapping
Flexible Formatting Supports multiple input and output date/time formats Easily handles date representations from different regions
Timezone Handling Supports conversion between different timezones Automatically handles timezone offsets for times
Type Safety Automatically converts strings to DateTime objects Ensures type consistency and correctness
Basic Usage
use Astral\Serialize\Attributes\Input\InputDateFormat;
use Astral\Serialize\Attributes\Output\OutputDateFormat;
use Astral\Serialize\Serialize;
class TimeExample extends Serialize {
// 输入时间格式转换
#[InputDateFormat('Y-m-d')]
public DateTime $dateTime;
// 输入时间格式转换
#[InputDateFormat('Y-m-d')]
public string $dateDateString;
// Output date format conversion
#[OutputDateFormat('Y/m/d H:i')]
public DateTime|string $processedAt;
// Supports multiple date/time formats
#[InputDateFormat('Y-m-d H:i:s')]
#[OutputDateFormat('Y-m-d')]
public string $createdAt;
}
// Create order object
$order = TimeExample::from([
'dateTime' => new DateTime('2023-08-11'), // Input format: Y-m-d
'dateDateString' => '2023-08-15', // Input format: Y-m-d
'processedAt' => '2023-08-16 14:30', // Default input format, also supports DateTime objects
'createdAt' => '2023-08-16 14:45:30' // Input format: Y-m-d H:i:s
]);
// Convert to array
$orderArray = $order->toArray();
// Content of $orderArray:
// [
// 'dateTime' => '2023-08-11',
// 'dateDateString' => '2023-08-15',
// 'processedAt' => '2023/08/16 14:30',
// 'createdAt' => '2023-08-16'
// ]Time Conversion with Timezone
最后更新于