-1

I need to retrieve a date of this format 2014-17-02 00:00:00 , where the hours minutes and seconds are just zeros, meaning I want today as the day started after midnight, or yesterday 2014-16-02 23:59:59pm depending on how you look at it.

The reason I want to retrieve this kind of datetime is so that I can effectively compare my 'created' field inside the database, allowing me to obtain records inserted 'today'..

Any suggestions? I have tried these:

$this->today = date('Y-m-d H:i:s');
 $this->today = date('Y-m-d H:i:s',strtotime('now'));

But they seem to do the same thing(give me datetime in accuracy of hh mm ss) which is not what I want.

I want to execute this query

"SELECT users.role as user_role, 
            Count(users.role) AS user_count
            FROM users
            WHERE users.created >= '$this->today' // any record created after yest midnight
            GROUP BY users.role"
LogixMaster
  • 586
  • 2
  • 11
  • 36

4 Answers4

3
$this->today = date('Y-m-d') . '00:00:00';

Would do the trick.

Rehan Aslam
  • 241
  • 3
  • 12
  • I went with what @CBroe said in his comment! both work though so marking as answered! Ofc concatenation! how did I not think of that one, simple yet effective! Thanks! – LogixMaster Feb 17 '14 at 10:43
1

It is also possible to calculate this purely using SQL - one less clock to worry about.

SQL - Yesterday's date

SELECT ....
WHERE users.created >= NOW() - INTERVAL 1 DAY
Community
  • 1
  • 1
BillyBigPotatoes
  • 1,330
  • 11
  • 23
  • +1 for pointing this out. Though it is not equivalent to `$this->today = date('Y-m-d') . '00:00:00';` or `$this->today = date('Y-m-d 00:00:00');` – LogixMaster Feb 17 '14 at 10:51
1
 SELECT users.role as user_role, 
 Count(users.role) AS user_count
 FROM users
 WHERE DATE(users.created) >= '$this->today'
 GROUP BY users.role
cornelb
  • 6,046
  • 3
  • 19
  • 30
0
 $this->today = date('Y-m-d H:i:s');

i think replace this with

 $this->today = date('Y-m-d', strtotime("-1 days")); // get yesterday date to 
wild
  • 340
  • 1
  • 3
  • 14
  • Logically, I think this wouldn't work for me as I need to obtain records created after midnight yesterday, and not records created yesterday. – LogixMaster Feb 17 '14 at 10:45
  • its give yesterday date and in your query get recorde >= yesterday date, it means you get whole record which is create today, i hope u are understand – wild Feb 17 '14 at 10:48
  • This will also retrieve records created yesterday. I think you meant to use the '>' n operator, rather than '>=' . I can see that would work! – LogixMaster Feb 17 '14 at 10:57
  • sorry it my foult i was forget to change this one >= to > – wild Feb 17 '14 at 11:01