0
$m = squery("SELECT date FROM attendance WHERE id=7" 

This query returns array of dates.. I Want to extract only month name from this array.. Thanxs.

10
  • What type of field is date? Commented Jun 28, 2018 at 6:54
  • What is the datatype of your date column? Commented Jun 28, 2018 at 6:54
  • stackoverflow.com/questions/1781946/… Commented Jun 28, 2018 at 6:54
  • select monthname( date )?? Commented Jun 28, 2018 at 6:55
  • @ RamRaider its returning array.. Commented Jun 28, 2018 at 7:00

3 Answers 3

3

Assuming your date column is of type date then,

Let a dummy table data be as follows,

Table

+------+------------+ | id | DDate | +------+------------+ | 7 | 2018-06-21 | | 7 | 2018-06-22 | | 7 | 2018-06-23 | | 7 | 2018-06-24 | | 7 | 2018-06-25 | | 7 | 2018-06-26 | +------+------------+ 

Query 1 : to get the month number

select month(DDate) as Mnth from MBO where id=7 group by Mnth; +------+ | Mnth | +------+ | 6 | +------+ 

Query 2 : To get month name

select monthname(DDate) as Mnth from MBO where id=7 group by Mnth; +------+ | Mnth | +------+ | June | +------+ 

You can change your SQL as above to assign your variable to desired value,

Note: it is assumed that for id=7 a single month dates would be present in table hence added a group by to get unique value. Else you may need to add more conditions to get a single value, or if you just need to get any month then a limit 1 will do.

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

1 Comment

Good to hear .. Happy to help .. :) @Sachin
1

Try this

echo $date = date('j M Y ', strtotime($row['date'])); 

day=j, month=M, year=Y

2 Comments

what is your output?
Its array with dates and with the month
1

Use DATE_FORMAT function

"SELECT DATE_FORMAT(`date` , '%M') AS `month_name` FROM `attendance` WHERE id=7 group by month_name" 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.