Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

1970-01-01 is getting inserted to database when no date is selected

Tags:

date

php

mysql

I have a text box where date is entered in php. Since I want the date to be in particular format I am using. date("d.m.Y", strtotime()) to conver the date. When no date is give or if I leave the textbox blank it will enter 1970-01-01 to database. How can I solve this? Here is my code

 $d_date= clean($_POST['datum']);
$newdate = date("Y-m-d", strtotime( $d_date));

If $d_date value is null the it will enter 1970-01-01 to database.

like image 989
user2672830 Avatar asked Jan 17 '26 14:01

user2672830


1 Answers

Make field in the database nullable, and when date is not submitted in the form set it to NULL. Default behaviour of mysql is to set date to default value, which is 1970-01-01(unix epoch)

like image 161
Agares Avatar answered Jan 20 '26 03:01

Agares



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!