Jump to content

Date format


scarlson

Recommended Posts

I am needing a way to display my DATES in a MMDDYY format.  I am using mySQL for the database and i am storing the info in a DATE type. 

 

I know there is a DATE_FORMAT() but not sure on how to use it or where to use it at.  Here is the code for my date field box:

 

<label>Start Date
              <input type="text" name="start_date" id="start_date" value="<?php echo $garage_sale_start_date ?>"/>
              </label>

 

Is the DATE_FORMAT() used here or at the query?

Link to comment
Share on other sites

depending on what date you are storing in the Database... you should be able to use the following to reorder the date as you want...

 

<?php

$old_date = strtotime($garage_sale_start_date);
$new_date = date("F j, Y", $old_date);         // using the php function date() to create new format
echo $new_date;

?>

 

http://us3.php.net/manual/en/function.date.php

Link to comment
Share on other sites

Ok, that works.  My next question is, is that the date is coming back as December 7, 2007.  What can I do so it's just:  12-07-2007?

 

Also, if the user wants to change this date at some point, do I have to reconvert the date back for mySQL?  If so, how?

 

Scott

Link to comment
Share on other sites

Look up the date() function at php.net and you will see how to display it differently.

 

As for changing it back, its just as easy to use date() again and put it back the same way.  But you may want to add validation to make sure it's in the right format if the user changes it, because what if they enter yr/mo/dd or mo/dd/yr, or some other unexpected format.

Link to comment
Share on other sites

Well when ever I try to update the date, it erases the data in the database and when i log back in the form should pre-load the data back and I always get this date:  30-11-1999

 

If I manually input the date into the database and then login, I get the correct results:

 

$start_date = $_POST['start_date'];

 

mysql_query("UPDATE garage_sale SET start_date = '$start_date' WHERE login_id = '$logid' ") or die(mysql_error());

 

Any ideas?

Link to comment
Share on other sites

When I use PHPs date() function I refer to this page for help with formatting it:

http://us2.php.net/date

 

I always store dates in my MySQL databases as unsigned integers using PHPs time() function to get the current date and time. When I get this back I only have to use date() to format it:

<?php
  $fetch=mysql_fetch_assoc(mysql_query("SELECT mydate FROM mytable"));
  echo date("d-M-Y H:i:s",$fetch['mydate']);
?>

 

Some prefer to store dates & times in databases using MySQLs own datatypes (to let MySQL do most of the work) but not being that savvy with MySQL, I find the above method easiest.

Link to comment
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.