0

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??

5
Contributors
6
Replies
38
Views
3 Years
Discussion Span
Last Post by diafol
1

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

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

1
$date1 = "$month/$day/$year";

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

1

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

Edited by almostbob

0

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.

Edited by matrixdevuk

0

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.

0

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');
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.