Jump to content

how to get the month in the date in set of records


phpmady

Recommended Posts

Hi,

 

I am having set of records,

id     

name

dob

 

1  philip        1278226800

2  winsent    1278216800

3  Benn          1272226800

 

 

now i want to fetch the records based on the month and listing out, can anyone help me to write for the above concept.

 

Thanks,

 

 

Link to comment
Share on other sites

Can you clarify that a little? Do you mean you want to list all the records, grouped or ordered by the month, or you want to list the records that are from a particular month?

 

 

Hi,

 

First of all Thanks for your reply, i want to list the records that are from a particular month

 

thanks,

Link to comment
Share on other sites

Use the msyql MONTH() function directly in your query. You will need to get the Unix Timestamp into a usable DATE format (why do people still bother to use a Unix Timestamp.) See the mysql FROM_UNIXTIME() function to do that.

 

Untested but should work -

 

$month = 9; // the month you want to find

$query = "SELECT * FROM your_table WHERE MONTH(FROM_UNIXTIME(your_Unix_Timestamp_column)) = $month";

 

If you want the current month, you could do that directly in the query -

 

$query = "SELECT * FROM your_table WHERE MONTH(FROM_UNIXTIME(your_Unix_Timestamp_column)) = MONTH(CURDATE())";

Link to comment
Share on other sites

I don't store UNIX timestamps, so I haven't tested this, but it should work, as far as I can tell.

 

$month = 5; // query is set up for numeric month representation . . .
$query = "SELECT `field_1`, `field_2` FROM `table` WHERE FROM_UNIXTIME(`timestamp_field`, '%m') = $month";

 

Link to comment
Share on other sites

Use the msyql MONTH() function directly in your query. You will need to get the Unix Timestamp into a usable DATE format (why do people still bother to use a Unix Timestamp.) See the mysql FROM_UNIXTIME() function to do that.

 

Untested but should work -

 

$month = 9; // the month you want to find

$query = "SELECT * FROM your_table WHERE MONTH(FROM_UNIXTIME(your_Unix_Timestamp_column)) = $month";

 

If you want the current month, you could do that directly in the query -

 

$query = "SELECT * FROM your_table WHERE MONTH(FROM_UNIXTIME(your_Unix_Timestamp_column)) = MONTH(CURDATE())";

 

Thanks,

 

Thanks i have made it using your query, and thanks for make me knowledgable in date functions.

 

Thanks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.