JonasT JonasT - 6 months ago 14
PHP Question

How to insert and read time/date in MySQL/PHP?

I want to read the single day, month and year, without adding 3 extra myql-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"

Answer

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

Comments