I read the csv file, got the date from there, checked - everything is fine. as I write to the database, the date is only for the first line 1970-01-01 03:00:00 And only the first line, it normally writes the rest (I still convert the date to the desired format)

Do so

foreach ($fil as $val) { $trd = date('Ymd H:i:s', strtotime($val[1])); mysql_query("INSERT INTO mytable(id, date) VALUES ('$val[0]', '$trd')") Or die(mysql_error()); } 
  • so in the first line of your csv, probably, the names of the columns. not? - splash58
  • What happens if print_r($trd) ? - blits
  • print_r ($ trd) is strange, but the first date is just 1970. what’s the problem then? Why does he just convert the first date incorrectly? - Kostya
  • do so and see what $i=0; foreach ($fil as $val) { if (! $++) echo $val."br"; $i=0; foreach ($fil as $val) { if (! $++) echo $val."br"; - splash58
  • nothing works at all? and the meaning of it then? - Kostya

1 answer 1

So in your first source line there is no date / strtotime () is not recognized as a date. To check, you can do this in the command line: php -r "echo date('Ymd H:i:s', strtotime('xyz'));"

'xyz' is the "wrong" date - strtotime returns false - and the date interprets this value as zero and displays Unix Epoch for your time zone.
Apparently your server is in the UTC + 3 zone, so you get 1970-01-01 03:00:00

Total : look at your source data. Maybe the first line you do not need to write to the database. Or correct the date there.