I am storing time using a unix timestamp integer for each booking in my database. I am having a problem where my query is not selecting things properly, and seems to be a day off.
For example, on my website I have a calendar table which shows each day of the month. I use the following code in order to populate products for each day:
$sql = "select b.*, p.name as p_name, p.color as p_color
from `bookings` as b
left join `products` as p on (p.id = b.id_product)
where date_format(from_unixtime(b.timestamp), '%M %e %Y') = date_format(from_unixtime(:timestamp), '%M %e %Y')
order by b.timestamp asc";
$stm = $this->app->db->prepare($sql);
$stm->bindParam(':timestamp', $this->timestamp);
$stm->execute();
$res = $stm->fetchAll();
Now, it is showing bookings I made for January 24th on the 25th day in my calendar.
The problem seems to lie with this part of the query:
where date_format(from_unixtime(b.timestamp), '%M %e %Y') = date_format(from_unixtime(:timestamp), '%M %e %Y')
For example, when $this->timestamp = '1453698000'
(the 25th) and b.timestamp = '1453618800'
(the 24th), it is showing this record on the 25th in my calendar.
I have absolutely no idea why this is happening. I've tried changing the date_format query to use '%d-%m-%Y', I've tried adding '00:00:00', I've tried manually setting the timezone in the PDO construct, I've made sure all my time zones are lining up correctly (which they are) by echoing the timestamps using the PHP date() function.
Why is this happening? Any help would be greatly appreciated.