1

I want to read the single day, month and year, without adding 3 extra MySQL-rows, in this format (with PHP):

day: 01
month: Jan, Feb, Mar..(first three letters)
year: 2011

This is table and PHP script, which I use now:

I add the date with PHP:

mysql_query("INSERT INTO news (...,`time`) VALUES (...'".date(d.".".m.".".Y)."');"); 

I read it out with:

$query = "SELECT * FROM news";
$result = mysql_query ($query);

while ($row = mysql_fetch_assoc ($result)) {
echo $row['time'];
}

MySQL table:

news:
time(text):
"27.03.2011"
1
  • I'm confused, why are you not just using one of MySQL's built in date/time types, like date? An internal MySQL date would be Y-m-d, not d.m.Y. Furthermore, you're using date() incorrectly - the date codes are in a string, so for this (which I'm not recommending, just correcting your code) it would be date('d.m.Y'). But there's no reason to do that. Commented Aug 5, 2011 at 15:31

5 Answers 5

1

Query should be:

mysql_query("INSERT INTO news (...,`time`) VALUES (...'".date(d.".".M.".".Y)."');");

M instead of m gives you the 3 letter textual representation of the month.

Get it with:

while ($row = mysql_fetch_assoc ($result)) {
    echo date( 'd', strtotime( str$row['time'] ) );
    echo date( 'M', strtotime( str$row['time'] ) );
    echo date( 'Y', strtotime( str$row['time'] ) );
}

Read more on:

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

Sign up to request clarification or add additional context in comments.

1 Comment

thnaks. that was the time-config i was searching for ;)
1

you can either do it in PHP, check the strftime function or use in SELECT something like

   SELECT DAY(date) as day, MONTH(date) as month, YEAR(date) as year FROM table

and in php you would acccess it as $result['day']. $result['month'] etc. the "date" in SELECT query is of course the name of the column in which you store your date. I would recommend strftime

Comments

1

You can use MONTH(), DAY(), YEAR() Mysql functions., i.e,

SELECT MONTH(`time`) AS `month`, DAY(`time`) AS `day`, YEAR(`time`) AS `year` FROM `news` [...]

Comments

0
SELECT *, substring(1,2) as day, substring(4,2) as month, substring(7) as year FROM table

Also you can(and should) use date table format and use DAY(), MONTH(), YEAR() functions

Comments

0

You should be using a DATETIME column in your mysql table. MySQL is then responsible for storing the date in its own internal format, and you can retrieve it in any format you need.

To insert the current date, you can simply

INSERT INTO news (...,`time`) VALUES (...,NOW())

To retrieve it in the format you want, use

SELECT DATE_FORMAT(`time`, '%d.%b.%Y');

Documentation on MySQL DATE_FORMAT()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.