Question

I am trying to grab any record with a ctime of today. For some reaosn, &tomorrow and &yesterday do not have any values. If I remove strtotime() their values are string representations of the date derived from mktime. What am I doing wrong with strtotime?

PHP:

public function fetchToday()
{
    $tomorrow  = strtotime(mktime(0, 0, 0, date("m")  , date("d")+1, date("Y")));
    $yesterday  = strtotime(mktime(0, 0, 0, date("m")  , date("d")-1, date("Y")));      
    $q = "SELECT * FROM $this->_table WHERE ctime < $tomorrow AND ctime > $yesterday";      
    return $this->fetchBySelect($q);
}

Error:

ModelFactory - 'ERROR: syntax error at or near "AND" LINE 1: SELECT * FROM motd WHERE ctime < AND ctime >

PostrgeSQL table field:

ctime timestamp(0) with time zone

Query:

SELECT * FROM motd WHERE ctime < 1372402800 AND ctime > 1372230000;

`

Was it helpful?

Solution

All answers so far focus on php side, but on the other hand, I would say that the proper solution is to use PostgreSQL features, and change the php code to:

public function fetchToday()
{
    $q = "SELECT * FROM $this->_table WHERE ctime < 'tomorrow'::timestamptz AND ctime > 'yesterday'::timestamptz;";      
    return $this->fetchBySelect($q);
}

OTHER TIPS

Why not just:

$tomorrow  = date("Y-m-d", strtotime("+1 day")); // sets tomorrow = 2013-06-28
$yesterday  = date("Y-m-d", strtotime("-1 day")); // sets tomorrow = 2013-06-26

This is assuming your ctime column is a MySQL datestamp. If it's a unix timestamp then:

$tomorrow  = strtotime("+1 day");
$yesterday  = strtotime("-1 day");

Since strtotime converts a param to a unix timestamp. Your code is redundant using both strtotime and mktime.

EDIT: Based on previous posts, looks like your ctime column is a unix timestamp, so my second example is the way to go. strtotime("+1 day") will return an int representing the unix timestamp of NOW + 1 Day, and reverse for -1 Day.

Using strtotime on the return value of mktime makes no sense. Both functions return a Unix timestamp, so use one or the other. I suggest strtotime because it is simple to use:

$tomorrow  = strtotime('tomorrow');
$yesterday = strtotime('yesterday');

Each of these returns the timestamp corresponding to the midnight before the day begins, which looks like what you want.

I think there are errors in these statements, so they are producing empty strings and make query to fail.

$tomorrow  = strtotime(mktime(0, 0, 0, date("m")  , date("d")+1, date("Y")));
$yesterday  = strtotime(mktime(0, 0, 0, date("m")  , date("d")-1, date("Y")));      

The mktime() returns unix timestamp and you don't need to use strtime().

It depends how you store data in your database. If they are kept as TIMESTAMP, just omit strtotime.

If you keep them as ISO like "YYYY-MM-DD" use date() only.

public function fetchToday()
{
    $tomorrow = strtotime("+1 day", strtotime(date("Y-m-d H:i:s"))); 
    $yesterday= strtotime("-1 day", strtotime(date("Y-m-d H:i:s"))); 
    $q = "SELECT * FROM ".$this->_table." WHERE ctime < ".$tomorrow." AND ctime > ".$yesterday;      
    return $this->fetchBySelect($q);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top