Jump to content

Archived

This topic is now archived and is closed to further replies.

hostfreak

Working with dates

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Can you please show some sample code to see if we can maybe point you in the direction?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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().

Share this post


Link to post
Share on other sites
Alright here is an example of how I would add it:

[code]
$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');"
[/code]

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
It's a fairly simple task, that has little to with dates, but more with strings:

[code]<?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];
?>[/code]

and on from there.

Share this post


Link to post
Share on other sites
After looking at the php manual I figured out why. Just needed a simple change:

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

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


Thanks.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

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.