These are the columns
active (either 0(open) or 1(closed))
begin column consists dates like 2012-01-01
i want to get the financial year begin and end date
SELECT CONCAT(BEGIN,'/',END) AS YEAR FROM fiscalyear WHERE active='0' ORDER BY `id` DESC LIMIT 1
THE ABOVE QUERY SELECT DATE IN THIS FORMAT: 2012-01-01/2013-12-31 BUT INSTEAD OF THIS I WANT TO PRINT THE SELETED DATE IN THIS FORMAT 2012-13..
If you want the last row, you can order the table in DESC order by id
which is auto increment.
SELECT `BEGIN`,`END`
FROM `fiscalyear`
WHERE `active`='0'
ORDER BY `id` DESC
LIMIT 1
It should be noted that BEGIN and END are reserved words in MySQL, and should be represented as fields using "`"
As for printing the date, I recommend PHP class DateTime
Order it by the id
primary key, and use a LIMIT 1
, so you only get the last record. Use the built in DATE_FORMAT
to format your date.
SELECT
CONCAT(DATE_FORMAT(`BEGIN`, '%Y'), '-',
DATE_FORMAT(`END`, '%Y')) AS mydates
FROM
`fiscalyear`
WHERE
`active` = 0
ORDER BY
`id` DESC
LIMIT 1
Edit: I changed the query to concat the years so it will output 2012-2013
. The result will be in mydates
. If you want the end year to be 13
instead of 2013
format, then use %y
a lowercase y
in the format string.
About your second question, you can define Begin and End in your database as DateTime (i remember some data type like this was defined in mySQL) and then you can use this function:
/* Analyzes time-date and returns mktime!*/
function analyze_time_date($tdstr)
{
// tdstr : 2010-08-12 11:41:14
// 0000000000111111111
// 0123456789012345678
$year = (int)substr($tdstr,0,4);
$month = (int)substr($tdstr,5,2);
$day = (int)substr($tdstr,8,2);
$hour = (int)substr($tdstr,11,2);
$minute = (int)substr($tdstr,14,2);
$second = (int)substr($tdstr,17,2);
return mktime($hour,$minute,$second,$month,$day,$year);
}
/* */
function format_time_date($tdstr,$format)
{
return date($format,analyze_time_date($tdstr));
}
when you wanna insert a new row to your database you should use 'date time field name' = NOW()
in your SQL query.
SELECT
CONCAT(DATE_FORMAT(`BEGIN`, '%Y'), '-',
DATE_FORMAT(`END`, '%y')) AS mydates
FROM 0_fiscal_year
WHERE closed
= 0 ORDER BY id
DESC LIMIT 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.