Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/63263-date-help/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/63263-date-help/#findComment-315336
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/63263-date-help/#findComment-315363
Share on other sites

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);

?>

Link to comment
https://forums.phpfreaks.com/topic/63263-date-help/#findComment-315542
Share on other sites

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>





 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/63263-date-help/#findComment-315590
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.