mysql时间戳转周,MYSQL将时间戳转换为月份

本文讨论了一个关于MySQL日期函数的问题,作者尝试比较当前月份与数据库中给定的时间戳,但遇到了时间戳月份判断不准确的问题。通过检查具体的时间戳值,发现其对应的月份并非预期的六月,而是五月。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I have this certain problem about mysql date functions.

I'm trying to compare the value of THIS MONTH to the given timestamp in database.

For example, month today is june, and the timestamp is 1369967316

And I'm trying to determine if that timestamp is in month of june.

$query = db_query("SELECT * FROM users WHERE MONTH(FROM_UNIXTIME(CURDATE())) = MONTH(1369967316)");

//count total members this mont

$members_month = $query->rowCount();

so if I used the rowCount, the $members_month should have the value of 1.

Unfortunately it doesn't work.

Any help would be appreciated.

Well I saw some answers that some kind of relevant to mine but it doesn't hit the spot or I didn't applied it well.

解决方案

This works for me:

mysql> SELECT MONTH(FROM_UNIXTIME(1369967316));

+----------------------------------+

| MONTH(FROM_UNIXTIME(1369967316)) |

+----------------------------------+

| 5 |

+----------------------------------+

Your issue is likely coming from the fact that 1369967316 is May 30th, not June (as you expect), thus resulting in an inequality with MONTH(CURDATE()).

mysql> SELECT FROM_UNIXTIME(1369967316);

+---------------------------+

| FROM_UNIXTIME(1369967316) |

+---------------------------+

| 2013-05-30 22:28:36 |

+---------------------------+

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值