my database feild type is timestamp here is my code I used it but It also gave me today date I need every date before today
"SELECT * FROM {mytable} WHERE date <".$now_timestamp." "
please help me Thank you
my database feild type is timestamp here is my code I used it but It also gave me today date I need every date before today
"SELECT * FROM {mytable} WHERE date <".$now_timestamp." "
please help me Thank you
You could just use strtotime()
to get yesterday's date instead of using a DateTime
object:
$yesterday = strtotime('-1 day');
$sql = 'SELECT * FROM {mytable} WHERE date < :timestamp';
$result = db_query($sql, array(':timestamp' => $yesterday));
Timestamp is basically how many seconds have passed since 1st Jan 1970 UTC. To get the exact timestamp of Yesterday at 23:59 hour 59th second I can think of the following approach:
$yesterday = strtotime(((string) date('d-m-Y', strtotime('-1 day', time()))) . ' 23:59:59');
It's not really elegant, but if you really need exact time, then the above code should do the trick.
A breakdown of what's happening is:
$now = time();
$yesterday = strtotime('-1 day', $now);
$yesterday_date = date('d-m-Y', $yesterday);
$yesterday_date_string = (string) $yesterday_date . ' 23:59:59';
$yesterday_timestamp = strtotime($yesterday_date_string);