Reputation: 559
Date and time from URL shall be in format YYYY-MM-DD-HH-II-SS. I want to check it correctness and insert into database in column of mysql timestamp format.
1) With $timestamp = $this->input->get('time_from')
I receive it from url.
2) Before insertion in database I try to print it.
if (! empty($timestamp)) {
$timestamp = date_create_from_format('Y-m-d-H-i-s', $timestamp);
if (! $timestamp)
echo date_format($timestamp, 'Y-m-d H:i:s');
}
3) I obtain that year could be before 1970, for example 1950-01-01-10-32-01 is correct by this checking. But it is impossible to insert such date in mysql column. 4) I obtain that 2012-02-31-10-00-00 is transformed automatically in 2012-03-02 10:00:00.
The questions are: how to avoid these confusing automatic transformations? What is the best way to provide such checking?
Upvotes: 0
Views: 683
Reputation: 30414
You cannot store a date before 1970 in a MySQL TIMESTAMP column. Use DATETIME instead. http://dev.mysql.com/doc/refman/5.0/en/date-and-time-types.html
You can use the checkdate() function to see if a date is valid.
Upvotes: 1