Hi, im trying to transform a string to make it a date

    $date1 = '$month/$day/$year'; //in jS value form type="number"
    $date2 = strtotime($date1); 
    $date3 = date('Y-m-d',$date2);

The problem is that when I SET date = $date3 the database saves 0000-00-00. what am I doing wrong??

What type of timestamp(timestamp, datetime, current_time) is your column excepting

Try using this"
date('Y-m-d H:i:s', strtotime($date2));

Member Avatar for diafol
$date1 = "$month/$day/$year";

Use double quotes or you're storing the literal strings otherwise.

the database is not supposed to be human readable
why not simplify everything and store the timestamp generated in line 2
sql date objects store in 4 bytes
text dates in the chosen format store in 14 bytes thats a lot of unneccessary wasted space in, for example, 1 million records 10MB
the date object is simpler to select between date1 and date2
the date object is order of magnitude faster
the date object is unique: 2014-12-2 can be 2 December or 12 February depending on who reads it
the date object can be simply parsed on output to whatever date format the user expects, by usewr choice.

faster smaller error-resistant user-friendly
what a good idea

You probably set the data type within the MySQL database to DATETIME or DATE, thus making MySQL accept Y:m:d H:i:s or default to its own method. If you were to change the date data type to VARCHAR(255) — it would work again as MySQL would have no default.

Hope this helps.

Hi, guys thankyou for your reply. I forgot about this thread when I got it working. Heres what I did:

$date1 = $month.'/'.$day.'/'.$year;
$date2 = strtotime($date1);
$date3 = date('Y-m-d', $date2);

I dont know how it worked but it did. My column is at default date. Again, Thank you guys.

Member Avatar for diafol

For completion's sake, you could also use the DateTime object:

$date1 = "$month/$day/$year";
$datex = new DateTime($date1);
echo $datex->format('Y-m-d');
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.