i would like add clause where - max 30 days from now.
in database i have timestamp:
2011-08-30 20:29:35
id | name | date
1 | aaa | 2011-08-30 20:29:35
2 | vvv | 2011-08-10 20:29:35
3 | bbb | 2011-07-10 20:29:35
4 | fff | 2011-08-14 20:29:35
5 | ddd | 2011-06-10 20:29:35
$query = Doctrine_Core::getTable('News')->createQuery('a');
$query->addWhere('date ????????');
How can i get all news recent 30 days?
$query->andWhere('date > ?', date('Y-m-d', time() - 60*60*24*30))
MySQL provides another handy solution :
WHERE date > DATE_SUB(NOW(), INTERVAL 30 DAY)
see : http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add
Related
Example my_table
ID | Name | Date
--------------------------
12 | John | 123456789
13 | Mike | 987654321
...
29 | Rick | 123498765
30 | Adam | 987651234
show output result like this
Month | Count
--------------------------
3 | 5 |
6 | 8 |
How can I do this with PHP?
You can do this using MySQL Query as below.
SELECT MONTH(FROM_UNIXTIME(`Date`)) `Month`
,COUNT(ID)
FROM my_table
GROUP BY `Month`;
Since post tagged codeigniter
This is codeigniter way:
$query = $this->db->select("month(from_unixtime(`Date`)) as `month`, count(1) as `count`",FALSE)
->group_by("month");
->get("your_table");
I have a table like this:
// Times
+----+-------------+
| id | timestamp | // echo date('d/m/Y', $time)
+----+-------------+
| 1 | 1448460315 | // 25/11/2015 -- today
| 2 | 1428440265 | // 07/04/2015
| 3 | 1418160365 | // 09/12/2014
| 4 | 1448460215 | // 25/11/2015 -- today
| 5 | 1438440265 | // 01/08/2015
| 6 | 1438340265 | // 31/07/2015
| 7 | 1438437265 | // 01/08/2015
| 8 | 1448370315 | // 24/11/2015 -- yesterday
| 9 | 1148370315 | // 23/05/2006
| 10 | 1447870315 | // 18/11/2015 -- last week ({11-18}/11/2015)
+----+-------------+
Note: All those number in timestamp column are made of time() function using PHP.
Now I want to know, how can I select all rows which are today Or all rows which are yesterday, or last week?* (it should be noted, in MySQL NOW() is the same with time()).*
For example:
// Times - Today
+----+-------------+
| id | timestamp | // echo date('d/m/Y', $time)
+----+-------------+
| 1 | 1448460315 | // 25/11/2015 -- today
| 4 | 1448460215 | // 25/11/2015 -- today
+----+-------------+
You would need to use the MySQL BETWEEN function
use PHP to get the timestamp from midnight of day to 11:59:59 of day
date_default_timezone_set('America/Chicago');
$day_begins = strtotime(date('Y-m-d 00:00:00', strtotime('today')));
$day_ends = strtotime(date('Y-m-d 11:59:59', strtotime('today')));
-- sql code will look like
SELECT id FROM table WHERE `timestamp` BETWEEN ($day_begins AND $day_ends)
With something like:
SELECT
id,
timestamp
FROM
table
WHERE
DATE_FORMAT(FROM_UNIXTIME(`timestamp`), '%y-%m-%d') = (DATE_FORMAT(NOW(), '%y-%m-%d') - INTERVAL 1 DAY)
This question already has answers here:
Average time in hours,minutes and seconds in php
(4 answers)
Closed 8 years ago.
I have this table on my database.
+--------- --------- ------------+
| id | time | date |
+--------- --------- ------------+
| 1 | 0:12 | 03/30/2014 |
--------- --------- ------------
| 2 | 0:14 | 04/09/2014 | <-- date today
--------- --------- ------------
| 3 | 0:32 | 04/09/2014 | <-- date today
--------- --------- ------------
| 4 | 0:24 | 04/09/2014 | <-- date today
--------- --------- ------------
The time is only minutes and seconds. I want to get the total SUM of the time and get the average. The average count is depending on how many new records are added today which is 3.
Record count/s for today (04/09/2014) = 3
New time records for today:
0:14
0:32
0:24
Total Time for today = 1:10
Average = 1:10 / 3
MySQL Solution:
select
avg( str_to_date( time, '%i:%s' ) ) 'avg t in sec',
date(date) date
from entries
group by 2;
Demo # MySQL 5.6.6 Fiddle
Results as observed on my console:
mysql> select
-> sum( str_to_date( time, '%i:%s' ) ) t,
-> date
-> from entries
-> group by 2;
+------+---------------------+
| t | date |
+------+---------------------+
| 12 | 2014-03-30 00:00:00 |
| 70 | 2014-04-09 00:00:00 |
+------+---------------------+
2 rows in set (0.00 sec)
mysql> select
-> avg( str_to_date( time, '%i:%s' ) ) 'avg t in sec',
-> date(date) date
-> from entries
-> group by 2;
+--------------+------------+
| avg t in sec | date |
+--------------+------------+
| 12.0000 | 2014-03-30 |
| 23.3333 | 2014-04-09 |
+--------------+------------+
2 rows in set (0.00 sec)
I have a table below named transaction_detail
id_transd | id_trans | id_cust | inputtime |
1 | 1 | 1 | 2013-04-15 16:55:58 |
2 | 1 | 1 | 2013-05-15 16:55:58 |
3 | 1 | 1 | 2013-06-15 16:55:58 |
4 | 2 | 2 | 2013-06-15 16:55:58 |
I want to get the amount of the inputtime (type : datetime) which have the same id_cust. I've done with the date, but I don't know how to calculate the time. This is my sql syntax to calculate the date :
select (date(max(inputtime)) - date (min(inputtime))) as total from transaction_detail where id_cust = '$idp'
Any help will be very appreciated. Thanks before.
Have a look in this website this might help what you need.
http://www.sqlusa.com/bestpractices/datetimeconversion/
Use TIMESTAMPDIFF function to get difference between two datetime values in units you want.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timestampdiff
Units could be: MICROSECOND (microseconds), SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, or YEAR.
I have a table with number of page views per day. Something like this:
+------+------------+------+----------+
| id | date | hits | mangaID |
+------+------------+------+----------+
| 4876 | 1331843400 | 132 | 13 |
+------+------------+------+----------+
| 4876 | 1331929800 | 24 | 236 |
+------+------------+------+----------+
| 7653 | 1331929800 | 324 | 13 |
+------+------------+------+----------+
I'm trying to get sum hits from last week with the below code:
SELECT sum(hits) as hits FROM om_manga_views WHERE DATE_SUB(CURDATE(),INTERVAL 1 week) <= date and mangaID = '13'
My problem is that I'm storing date as time using strtotime in date's field as int type.
So how can i get what i want!?
Try this:
select sum(hits) hitCount from t
where from_unixtime(date) >= current_date() - interval 1 week and mangaId = 11
Here is the fiddle to play with.
I slightly changed your data because the records you provided are older than 7 days, so the sum would return 0.