简体   繁体   中英

PHP- working with date and time

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"
$this->today = date('Y-m-d') . '00:00:00';

会做到的。

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
 SELECT users.role as user_role, 
 Count(users.role) AS user_count
 FROM users
 WHERE DATE(users.created) >= '$this->today'
 GROUP BY users.role
 $this->today = date('Y-m-d H:i:s');

我认为用

 $this->today = date('Y-m-d', strtotime("-1 days")); // get yesterday date to 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM