The fputcsv function outputs data to a file in comma-separated format. Since CSV files are essentially plain text, all fields are saved as strings. Therefore, directly writing PHP DateTime objects or fields containing date and time into a CSV file may encounter the following issues:
Inconsistent Formats: Different systems or programs reading the CSV file might not correctly recognize the format of the date-time fields.
Data Loss or Errors: Some date-time formats may lose precision when saved, especially dates including timezone information.
To avoid these problems, it is necessary to properly format the date-time fields before writing.
When using fputcsv to write fields containing date and time, the most common approach is to use the format method of the DateTime object to convert the date-time into a consistent string format. For example, the ISO 8601 format (Y-m-d H:i:s) is generally a universal and compatible choice.
Assuming we have a DateTime object that we want to write to a CSV file, we can use the following code:
$date = new DateTime();
$formattedDate = $date->format('Y-m-d H:i:s'); // Formats to '2025-07-27 14:45:00'
This formats the current date and time into a string in the YYYY-MM-DD HH:MM:SS format.
If the date-time field is a string (such as retrieved from a database), we can also use the DateTime::createFromFormat method to convert and format the date. For example:
$dateString = '2025-07-27 14:45:00';
$date = DateTime::createFromFormat('Y-m-d H:i:s', $dateString);
$formattedDate = $date->format('Y-m-d H:i:s');
Next, let's see how to write the formatted date-time to a CSV file using fputcsv. Suppose we have an array containing multiple fields, with one field being a date-time field.
$data = [
['Name', 'Date', 'Amount'],
['John Doe', '2025-07-27 14:45:00', 1000],
['Jane Smith', '2025-07-28 09:30:00', 1500]
];
<p>$file = fopen('data.csv', 'w');</p>
<p>// Write header row<br>
fputcsv($file, $data[0]);</p>
<p>// Write data rows, ensuring correct date formatting<br>
foreach ($data as $row) {<br>
$row[1] = (new DateTime($row[1]))->format('Y-m-d H:i:s'); // Format the date field<br>
fputcsv($file, $row);<br>
}</p>
<p>fclose($file);<br>
In this example, the second column (Date) is a date-time field. Before writing to the file, we use the format method of the DateTime object to format the date as Y-m-d H:i:s. This ensures that no matter which system reads the CSV file, the date will remain in a consistent format, reducing the risk of format inconsistencies.
Timezone Issues: If your date-time includes timezone information, it is recommended to use the ISO 8601 format (Y-m-d\TH:i:sP), where P outputs the timezone offset. For example: 2025-07-27T14:45:00+00:00. Ensure timezones are handled correctly during formatting to avoid conversion problems.
CSV File Encoding: Make sure to save the CSV file with the correct encoding format. UTF-8 encoding is recommended to ensure special characters (such as Chinese characters) are saved correctly.
Date-Time Validity Checks: Before formatting, it’s best to validate the date-time string's validity to avoid formatting errors caused by invalid dates (for example, 2025-02-30).