Jump to content


Photo

Working with dates


  • Please log in to reply
10 replies to this topic

#1 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 26 July 2006 - 10:44 PM

I have a date field that I insert dates into in my mysql database. The default is 0000-00-00, when I insert the date I insert it as variablename with the values as variablenameY-variablenamem-variablenamed. That's all fine, but when I got to edit it as it is y-m-d as is expected. However I want to be able to edit it from a drop down (which is how I add that dates, from a dropdown). So what I need to figure out is how to extract the values from the field after they are inserted. Hope that makes sense. Thanks in advance.

#2 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 26 July 2006 - 10:54 PM

Can you please show some sample code to see if we can maybe point you in the direction?
PHP Ninja

#3 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 26 July 2006 - 10:55 PM

Also...I suggest you store dates in your db as timestamps...and then do any formatiing in the actual code for display purposes. Makes life easier.
PHP Ninja

#4 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 26 July 2006 - 10:58 PM

you can use the php function strtotime() to convert a date into a timestamp. you can also format the date within a mysql query using DATE_FORMAT().
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#5 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 27 July 2006 - 01:05 AM

Alright here is an example of how I would add it:

$connection = mysql_connect("$server","$user","$password"); 
mysql_select_db ($database);

$dateY = $_POST['dateY'];
$datem = $_POST['datem'];
$dated = $_POST['dated'];

$query = "INSERT INTO users(date)  VALUES('$dateY-$datem-$dated');"

Then I would make a form that has three dropdown fields. One being for the dateY (year), second being for datem (month), and the third being for dated (day). Then it would insert it into the mysql date field as year-month-day (0000-00-00). So when I go to edit it, I have to edit it in a input field as: 0000-00-00 . I would like to be able to edit it from a dropdown, but I need some way to extract the year-month-day values from the date.

#6 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 27 July 2006 - 01:07 AM

I thought about converting it, but that still doesn't allow me to edit it from a dropdown. Having three different ones: Year, Month, Date. I could just make three different fields in the database, but I've already got a lot added like this so was looking for some ideas before I do that.

#7 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 27 July 2006 - 07:10 AM

bump, thanks in advance for any new answers.

#8 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 27 July 2006 - 07:56 AM

It's a fairly simple task, that has little to with dates, but more with strings:

<?php
$res = mysql_query('SELECT date FROM users WHERE user='.$usr) or die ('Query Failed: '.mysql_error())
$row = mysql_fetch_assoc($res);
$arr = explode($row['date'],'-');

$y = $arr[0]; $m = $arr[1]; $y=$arr[2];
?>

and on from there.

#9 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 27 July 2006 - 09:49 AM

Hmm, doesn't seem to work.

#10 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 27 July 2006 - 09:55 AM

After looking at the php manual I figured out why. Just needed a simple change:

$array = explode('-', $row['date']);

$Y = $array[0]; 
$m = $array[1]; 
$d = $array[2];


Thanks.

#11 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 27 July 2006 - 09:37 PM

Oops, sorry about that.. You tend to get lazy about remembering the order of arguments when you always have dreamweaver make it soooo easy for you...  :P




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users