Beauford Posted August 4, 2007 Share Posted August 4, 2007 I am trying to get a date function working. I need to input a date into a mysql db with the format of - Monday January 15, 2007 - or Mon Jan 15, 2007. I have checked out the PHP manual, but it ain't helping. I have tried date("D M j Y") (and other combinations) in my code, but what format does the user have to enter into the form. No matter what I enter I get an error like the following. Incorrect date value: 'Fri Aug 3 2007' for column 'eventdate' at row 1. Am I even in the right ballpark? I need the user to enter a time as well - e.g. 5:00pm -, but I haven't even looked at that yet. Any help is appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/63263-date-help/ Share on other sites More sharing options...
cyber_ghost Posted August 4, 2007 Share Posted August 4, 2007 guy.. i just a noob... in php but.. try to hear my advice... try to change the field type to varchar... and you can format time as you wish... Quote Link to comment https://forums.phpfreaks.com/topic/63263-date-help/#findComment-315334 Share on other sites More sharing options...
dbillings Posted August 4, 2007 Share Posted August 4, 2007 You are probably trying to insert a string into a mysql value that is set for accepting dates. I would recomend using a unix time stamp (providing all of your dates are post 1969). Have a look at the time() function. http://us.php.net/manual/en/ref.datetime.php Quote Link to comment https://forums.phpfreaks.com/topic/63263-date-help/#findComment-315335 Share on other sites More sharing options...
dbillings Posted August 4, 2007 Share Posted August 4, 2007 Further explanation.... The mysql date field type will only accept dates of a given format, I can't recall the exact format something like 2007:08:03. Your selected format doesn't support that so hence the error. Your gut may tell you I'll just change it to a varchar and problem solved, but this will limit the versatility you have with the date. If you use a unix time stamp (which calculates the time in seconds from the unix epoch on Jan. 1, 1970) it will allow you to do all kinds of functional things like sort by date, calculate span of time between two dates, and times. Quote Link to comment https://forums.phpfreaks.com/topic/63263-date-help/#findComment-315336 Share on other sites More sharing options...
cyber_ghost Posted August 4, 2007 Share Posted August 4, 2007 thank you dbillings for this clarification...... it is better to save into a datetime field type... and you do the formatting of date during the coding... Quote Link to comment https://forums.phpfreaks.com/topic/63263-date-help/#findComment-315341 Share on other sites More sharing options...
Beauford Posted August 4, 2007 Author Share Posted August 4, 2007 Further explanation.... The mysql date field type will only accept dates of a given format, I can't recall the exact format something like 2007:08:03. Your selected format doesn't support that so hence the error. Your gut may tell you I'll just change it to a varchar and problem solved, but this will limit the versatility you have with the date. If you use a unix time stamp (which calculates the time in seconds from the unix epoch on Jan. 1, 1970) it will allow you to do all kinds of functional things like sort by date, calculate span of time between two dates, and times. In MySQL i have the date field as DATE and the time field as TIME, which are the built in functions for MySQL. I'm still confused as to what the user needs to enter to get the date and time formated as stated in my original message. when I go to http://us.php.net/manual/en/ref.datetime.php I eventually get back to what I already have. Can you clarify a little further. I think I'm having one of those brain cramps. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/63263-date-help/#findComment-315363 Share on other sites More sharing options...
dbillings Posted August 4, 2007 Share Posted August 4, 2007 Ok here we go... Set your mysql field to accept int instead of date, you won't need a seperate field for date and time it will be stored in one value. If you need to insert the current time mysql has a unix_timestamp() function that inputs up to the second. The following code will generate a unix timestamp for any date after Jan. 1 1970 up until ??? I'm not sure but well past 2010. <?php IF(isset($_REQUEST['submit'])) { $m = $_REQUEST['month']; $d = $_REQUEST['day']; $y = $_REQUEST['year']; $h = $_REQUEST['hour']; $m = $_REQUEST['minute']; // gmmktime the first zero is for seconds the last is a boolean for daylight savings time. $timestamp = gmmktime($h, $m, 0, $m, $d, $y, 0); } $month = range(1, 12); $day = range(1, 31); $year = range(1995, 2010); $hour = range(0, 24); $minute = range(1, 59); <form method="post" action="<?php $_SERVER['PHP_SELF']; ?>"> <label>Month</label><select name="month"> <?php foreach ($month as $int) { echo "<option value='$int'>$int</option>"; } ?> </select> <label>Day</label><select name="day"> <?php foreach ($day as $int) { echo "<option value='$int'>$int</option>"; } ?> </select> <label>Year</label><select name="year"> <?php foreach ($year as $int) { echo "<option value='$int'>$int</option>"; } ?> </select> <label>Hour</label><select name="hour"> <?php foreach ($hour as $int) { echo "<option value='$int'>$int</option>"; } ?> </select> <label>Minute</label><select name="minute"> <?php foreach ($minute as $int) { echo "<option value='$int'>$int</option>"; } ?> </select> <input type="submit" value="Submit> </form> Now your going to need to be able retrieve that date and turn the timestamp back into a useable date that you and I can read. run your query to fetch the date and insert it into the following function <?php echo date("D M j, Y At g:i", $timestamp); ?> Quote Link to comment https://forums.phpfreaks.com/topic/63263-date-help/#findComment-315542 Share on other sites More sharing options...
dbillings Posted August 4, 2007 Share Posted August 4, 2007 Try this one.... Can't get the hour to work out correctly though??? <?php IF(isset($_REQUEST['submit'])) { $mo = $_REQUEST['month']; $d = $_REQUEST['day']; $y = $_REQUEST['year']; $h = $_REQUEST['hour']; $m = $_REQUEST['minute']; // gmmktime the first zero is for seconds the last is a boolean for daylight savings time. $timestamp = mktime($h, $m, 0, $mo, $d, $y, 0); echo date("D M j, Y g:i A", $timestamp); } $month = range(1, 12); $day = range(1, 31); $year = range(1995, 2010); $hour = range(0, 24); $minute = range(0, 59); ?> <form method="post" action="<?php $_SERVER['PHP_SELF']; ?>"> <label>Month</label><select name="month"> <?php foreach ($month as $int) { echo "<option value='$int'>$int</option>"; } ?> </select> <label>Day</label><select name="day"> <?php foreach ($day as $int) { echo "<option value='$int'>$int</option>"; } ?> </select> <label>Year</label><select name="year"> <?php foreach ($year as $int) { echo "<option value='$int'>$int</option>"; } ?> </select> <label>Hour</label><select name="hour"> <?php foreach ($hour as $int) { echo "<option value='$int'>$int</option>"; } ?> </select> <label>Minute</label><select name="minute"> <?php foreach ($minute as $int) { echo "<option value='$int'>$int</option>"; } ?> </select> <input type="submit" name="submit" value="Submit"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/63263-date-help/#findComment-315590 Share on other sites More sharing options...
dbillings Posted August 4, 2007 Share Posted August 4, 2007 I'm guessing due to DST. Quote Link to comment https://forums.phpfreaks.com/topic/63263-date-help/#findComment-315594 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.