MyBB Community Forums

Full Version: How to Get Current Month Record using timestamp?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
	$first_day=strtotime(date('m-01-Y'));
	$last_day =strtotime(date('t-m-Y'))+(60*60*24);

this is my code but its not working, what's wrong?? found it from google results and it was working fine when i tested but not working with SQL

this is my query:
$q=$db->write_query("SELECT count(a.sid) as total, a.referrer,b.dateline,b.username from `".TABLE_PREFIX."newpoints_ar_pl` a inner JOIN `".TABLE_PREFIX."posts` b on a.pid=b.pid WHERE a.referrer='".$mybb->user['uid']."' AND b.uid='".$uid."' AND b.dateline>='".$first_day."' AND b.dateline<='".$last_day."'");

Problem with code:
its showing previous month data too...
change
$last_day =strtotime(date('t-m-Y'))+(60*60*24);
to
$last_day =strtotime(date('m-t-Y'));

you already get the last day of the month with "t" in the date string so you dont need to calculate it.
(2017-05-02, 10:41 AM)broatcast Wrote: [ -> ]change
$last_day =strtotime(date('t-m-Y'))+(60*60*24);
to
$last_day =strtotime(date('m-t-Y'));

you already get the last day of the month with "t" in the date string so you dont need to calculate it.

last day but not 23:59 hours of the last day, i'll get starting time of the last day that's why i add this calculation..
correct me if i am wrong??
With this caluculation you count from eg 1.1.2017 to 1.2.2017... not +23:59 .... you adding 24:00 ...
for the same result you can "+1 month" in strtotime.
(2017-05-02, 11:30 AM)broatcast Wrote: [ -> ]With this caluculation you count from eg 1.1.2017 to 1.2.2017... not +23:59 .... you adding 24:00 ...
for the same result you can "+1 month" in strtotime.

ohh. thanks, i will try this...
why dont you use TIME_NOW ?

i mean

$last_day =TIME_NOW + 60*60*24;

and i put in your query with timestamp - to search results.

like where b.dateline>=$timesearch

where $timesearch have this value before the query.

$timesearch = TIME_NOW - 60*60*24;

i use that to search items between last 24 hours.

see yah !!!
time now will not give him the first day of a month
@boatcast what part of i use to get last 24 hours you did not understand ?

it is inly a sample i did not read entire code but you can use the query code getting tinestamp of today and then get month and compare with db stamp where day was 1 only.

It can be done in several ways we only provide samples.

only we can provide entirely support with hands on code.

And i have no idea of what you are talking about so only i try to figure out.

How to Get Current Month Record using timestamp? I think he can get that with the timestamp about TIME_NOW reference and taken between 24 hours and query runs only every 1st of month any other case and at first load it will store into cache to not loads unnecesary queries. But is not clear at all without entire code and plugins to know how to use propertly. It is only an idea, because you have to load in database directly timestamp and you have to analize and do a query to get only that instances and not any other instance, for me is more easy take the latest 24 hours if day is 1, otherwise load into cache stored data or i dunno....