Altec Posted April 26, 2009 Share Posted April 26, 2009 I am having trouble getting dates to store in my MySQL database. The field is a TINYINT(20) which should store timestamps. Here is my form: case 'post': $current_month = date("F"); $current_date = date("d"); $current_year = date("Y"); $current_time = date("h:i"); ?> <p>Use the form below to add a new blog post to the main page.</p><br /><br /> <fieldset> <legend>Add New Blog Post</legend> <form action="./post_blog.php" method="post"> <label for="month">Date</label> <select name="month" id="month"> <option value="<?php echo $current_month; ?>"><?php echo $current_month; ?></option> <option disabled="true">----------</option> <option value="January">January</option> <option value="February">February</option> <option value="March">March</option> <option value="April">April</option> <option value="May">May</option> <option value="June">June</option> <option value="July">July</option> <option value="August">August</option> <option value="September">September</option> <option value="October">October</option> <option value="November">November</option> <option value="December">December</option> </select> <input type="text" name="date" id="date" size="2" value="<?php echo $current_date; ?>" /> <select name="year" id="year"> <option value="<?php echo $current_year; ?>"><?php echo $current_year; ?></option> <option disabled="true">----------</option> <option value="2009">2009</option> <option value="2010">2010</option> <option value="2011">2011</option> <option value="2012">2012</option> <option value="2013">2013</option> </select> <input type="text" name="time" id="time" size="5" value="<?php echo $current_time; ?>" /><br /><br /> <label for="title">Title</label><input type="text" id="title" name="title" size="60" /><br /><br /> <label for="content">Content</label><textarea id="content" name="content" cols="70" rows="15"></textarea><br /><br /> <input type="submit" name="submit" value="Post Blog" class="button" /> <input type="button" value="Cancel" onclick="javascript:window.location='./index.php'" /> </form> </fieldset> <?php break; Here is post_blog.php: if($_SERVER['REQUEST_METHOD'] == "POST") { db_connect('phreakyo_inertia'); foreach($_POST as $name => $value) { $d[$name] = clean($value,'mysql'); if(empty($d[$name])) { $error = 'You left the field "'.$name.'" blank.'; } } if(isset($error)) { echo '<p>'.$error.'</p>'; } else { $timestamp = strtotime($d['month'].' '.$d['date'].' '.$d['year'].' '.$d['time']); $d['content'] = nl2br($d['content']); $query = "INSERT INTO `blog` (title,content,timestamp) VALUES ('{$d['title']}','{$d['content']}','{$timestamp}')"; if(!mysql_query($query)) { echo 'Error: '.mysql_error(); } else { echo '<br /><span><strong>Blog post inserted successfully.</strong><br /><br />[ <a href="./index.php">Back to ACP</a> ]</span>'; } } } Here is my display script: db_connect('phreakyo_inertia'); $query = mysql_query("SELECT * FROM `blog` ORDER BY `timestamp` DESC"); if(!$query) { echo 'Error: '.mysql_error(); } else { while($row = mysql_fetch_array($query)) { foreach($row as $name => $value) { $d[$name] = stripslashes($value); } $d['timestamp'] = date("l F d Y", $row['timestamp']); echo '<div class="post-container">'."\n"; echo ' <div class="post-title row1">'.$d['title'].'<span class="right">'.$d['timestamp'].'</span></div>'."\n"; echo ' <div class="post-content row2">'.autop($d['content']).'</div>'."\n"; echo '</div>'."\n"; } } The value in phpMyAdmin shows as "127" and the date shown is "Wednesday December 31 1969." How can I fix this? Quote Link to comment https://forums.phpfreaks.com/topic/155758-dates-do-not-store/ Share on other sites More sharing options...
premiso Posted April 26, 2009 Share Posted April 26, 2009 Change TINYINT to INT and it should work. Quote Link to comment https://forums.phpfreaks.com/topic/155758-dates-do-not-store/#findComment-819876 Share on other sites More sharing options...
ohdang888 Posted April 26, 2009 Share Posted April 26, 2009 change the date and see what happens. EPOCH was January 1, 1970. it might have meant -127 second value..which puts it dec 31 1969 in that value...so maybe the negative wasn't being stored. Just try today's date and see what happens... Quote Link to comment https://forums.phpfreaks.com/topic/155758-dates-do-not-store/#findComment-819879 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.