473,498 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Another MySQL date question

I am entering some older things in a database for the first time.
Included in the information I need to enter is the month and year the
item was submitted. Ideally I'd like to be able to enter the old dates
into a field like 03/98 or 12/02 or and still be able sort them by
descending or ascending order.

Is there a relatively easy way to do this? and what kind of field (date,
text, etc) does it need to go into?

Thanks

Jul 17 '05 #1
8 1769
JDJones wrote:
I am entering some older things in a database for the first time.
Included in the information I need to enter is the month and year the
item was submitted. Ideally I'd like to be able to enter the old dates
into a field like 03/98 or 12/02 or and still be able sort them by
descending or ascending order.

Is there a relatively easy way to do this? and what kind of field (date,
text, etc) does it need to go into?


I'd put that into a `datetime` column with the day always being 1.

If you don't want that, the second choice would be *two* columns; one
for the year and another for the month, both "int".

Depending on your specific needs, formatting would then be done either
within the select or through php.
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #2
I noticed that Message-ID: <euQ0c.434100$I06.4906188@attbi_s01> from
JDJones contained the following:
I am entering some older things in a database for the first time.
Included in the information I need to enter is the month and year the
item was submitted. Ideally I'd like to be able to enter the old dates
into a field like 03/98 or 12/02 or and still be able sort them by
descending or ascending order.


Pick an arbitrary date in the middle of the month and add that to the
entry. e.g

$date= $year."/".$month."/15"

I may well store the date in UNIX format, data type integer, mainly
because I'm finding them slightly easier to work with.

Say you have

$year="97"; $month="03";

Get the date like this
$unixdate= strtotime($year.$month."15");

to display the date

print date("F, Y",$unixdate);
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #3
Geoff Berrow wrote:
I noticed that Message-ID: <euQ0c.434100$I06.4906188@attbi_s01> from
JDJones contained the following:

I am entering some older things in a database for the first time.
Included in the information I need to enter is the month and year the
item was submitted. Ideally I'd like to be able to enter the old dates
into a field like 03/98 or 12/02 or and still be able sort them by
descending or ascending order.

Pick an arbitrary date in the middle of the month and add that to the
entry. e.g

$date= $year."/".$month."/15"

I may well store the date in UNIX format, data type integer, mainly
because I'm finding them slightly easier to work with.

Say you have

$year="97"; $month="03";

Get the date like this
$unixdate= strtotime($year.$month."15");

to display the date

print date("F, Y",$unixdate);


After experimenting with you suggestion and Pedro's, I think the easiest
for me to use would be using the data type "date" column.

What if I decide to store it like that using the defaulted "2000-09-15"
format. How can I pull the numbers out to get it to print the result as
"09-2000" ?

Jul 17 '05 #4
JDJones wrote:
What if I decide to store it like that using the defaulted "2000-09-15"
format. How can I pull the numbers out to get it to print the result as
"09-2000" ?


Do it either on the SQL side:
( see http://www.mysql.com/doc/en/Date_and...functions.html )

mysql> select date_format(now(), '%m-%Y');
+-----------------------------+
| date_format(now(), '%m-%Y') |
+-----------------------------+
| 03-2004 |
+-----------------------------+
1 row in set (0.00 sec)
or on the PHP side:
( see http://www.php.net/date )

echo date('m-Y', $db_date);
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #5
Pedro Graca wrote:
JDJones wrote:
What if I decide to store it like that using the defaulted "2000-09-15"
format. How can I pull the numbers out to get it to print the result as
"09-2000" ?

Do it either on the SQL side:
( see http://www.mysql.com/doc/en/Date_and...functions.html )

mysql> select date_format(now(), '%m-%Y');
+-----------------------------+
| date_format(now(), '%m-%Y') |
+-----------------------------+
| 03-2004 |
+-----------------------------+
1 row in set (0.00 sec)
or on the PHP side:
( see http://www.php.net/date )

echo date('m-Y', $db_date);


I can't get either one to work. :(

I have the dates entered in a db date column called "submitted". It is
in the 0000-00-00 format. I am executing the query like: $query =
"select * from table where category = 'text' order by submitted DESC";

Then, cutting out extraneous portions of the script I am printing it by
doing this:

<?php
while ($row= mysql_fetch_array($result)) {
$title1 = $row["text"];
$title2 = $row["submitted"];
?>

<table border="2" width="100%" cellspacing="2" callpadding="0"
class="tableblue">
<tr>
<td width="100%"><? echo "<br><span
class=\"number\">$count.)</span>&nbsp;$title1&nbsp;$title2<br><br>"?></td>
</tr>
</table>

I can't get either of your two suggested methods to work. The echo
date('m-Y', $db_date); one output the correct format I was seeking when
I modified it - echo date('m-Y', $title2); - but everything was coming
up 12-1969 no matter what the dates were that were actually in the
field. What am I still doing wrong?


Jul 17 '05 #6
I noticed that Message-ID: <9%91c.463139$na.1097057@attbi_s04> from
JDJones contained the following:
I can't get either of your two suggested methods to work. The echo
date('m-Y', $db_date); one output the correct format I was seeking when
I modified it - echo date('m-Y', $title2); - but everything was coming
up 12-1969 no matter what the dates were that were actually in the
field. What am I still doing wrong?


You should have paid more attention to my reply <g>

echo date('m-Y', strtotime($title2));
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #7
Geoff Berrow wrote:
I noticed that Message-ID: <9%91c.463139$na.1097057@attbi_s04> from
JDJones contained the following:

I can't get either of your two suggested methods to work. The echo
date('m-Y', $db_date); one output the correct format I was seeking when
I modified it - echo date('m-Y', $title2); - but everything was coming
up 12-1969 no matter what the dates were that were actually in the
field. What am I still doing wrong?

You should have paid more attention to my reply <g>

echo date('m-Y', strtotime($title2));


(*hangs head sheepishly*)

You're right Geoff, I should have. I guess seeing the $unixdate scared
me off. ;)

It's now working and I thank you.

Jul 17 '05 #8
I noticed that Message-ID: <nnb1c.26112$ko6.231064@attbi_s02> from
JDJones contained the following:
echo date('m-Y', strtotime($title2));
(*hangs head sheepishly*)

he he...
You're right Geoff, I should have. I guess seeing the $unixdate scared
me off. ;)
It's just a number. The number of seconds since 1st January 1970.
Nothing scary really. :-)
It's now working and I thank you.


Cool, no problem.

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
15954
by: Dariusz | last post by:
I am a beginner in PHP and MySQL, and am working through a book and various online tutorials on PHP and MySQL and now stuck - installed everything on "localhost" and it all works fine. My question...
4
4406
by: Neil | last post by:
Hi, I hope this question isn't too far off topic....I'm almost at my wits end trying to figure this out. I have a Mysql database and I wish to automate the backup of the database because I...
5
3714
by: Dominique Javet | last post by:
Hello, I'm new to php and mysql and I use Dreamweaver MX 2004, so sorry for this "newbie" question... I've found no answer in the forum ... I've a date problem with my formular. In my mysql DB...
19
8674
by: Westcoast Sheri | last post by:
To keep track of how many fruits my visitors buy, I use a mySQL database (2 columns: "fruit" and "quantity")....so can we make these following mySQL queries work somehow? (visitor buys 5...
7
6088
by: JJ | last post by:
How do I set one field to have the updated timestamp, and another to have the created timestamp? I want to do this directly from code generated from DB Designer if possible?! JJ
3
7682
by: Joshepmichel | last post by:
Please to help me to following problem I want to do this 1. create Table Name MEMBER on the Database Name "mytestdb", 2. Add the Values to the Table through the Key board Inputs during running...
6
3923
by: Geoff Cox | last post by:
Hello, at the moment I can add the combined date and time into MySQL using php $dt1 = date("Y-m-d H:i:s"); is it possible to add the date and time separately? I thought it might be
4
2410
by: Michael Sharman | last post by:
Hi guys, I'm a little confused with dates. Ok, all I want to do is store a date in MySQL as a datetime object and be able to read and format it using PHP. My datatype in MySQL is DATETIME and...
3
9678
oranoos3000
by: oranoos3000 | last post by:
hi i work with php and mysql on the os windows i want to using function strtotime for compute diffrences between two date , one with format date("Y-F-d") and another date is current time and...
0
6993
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7162
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7197
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6881
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7375
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5456
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4899
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4584
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1411
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.