I try to get away from storing dates as MySQL dates.
Store the timestamps into the database as integers and use the PHP date function to convert it back your desired date format.
echo date('Y - m - D', mktime());
To insert
$timestamp = mktime(0,0,0,0,$_POST['month'],$_POST['day'],$_POST['year']);
$concert = $_POST['concert'];
$sql = "INSERT INTO concerts (concert, concert_timestamp)
VALUES ('$concert', '$timestamp');
mysql_query($sql,$dbConn);
To retrieve
$sql = "SELECT concert, concert_timestamp
FROM concerts
ORDER BY concert_timestamp ASC";
$result = mysql_query($sql,$dbConn);
$row = mysql_fetch_assoc($result);
$concert = $row['concert'];
$concert_date = date('j-m-Y',$row['concert_timestamp']);
The advantages are these:
- It's faster - mysql doesn't need to convert it to unixtimestamp when inserting or updating timestamp records.
- It's universal - although highly unlikely, if you needed to migrate to another database format like say Postgres, date transformation will not be a problem
- No more worrying about date format when insert into db
- You can format your date/time output with one php call instead of imploding the date given by mysql.
//full date - 12-06-2006
$date = date('j-m-Y',$row['concert_timestamp']); // you can use date to format the output
//broken up into day month year
$day = date('j',$row['concert_timestamp']);
$month = date('m', $row['concert_timestamp']);
$year = date('Y', $row['concert_timestamp']);
VS
//full date - 2006-06-12
$date = $row['concert_timestamp'];
//broken up into day month year
//get pieces or you ould use strtotime to convert back to timestamp
$date = implode('-',$row['concert_timestamp']);
$day = $date[2];
$month = $date[1];
$year = $date[0];
Hope this helps.
Regards,