Ash Ash - 3 months ago 9
PHP Question

How to round unix timestamp up and down to nearest half hour?

Ok so I am working on a calendar application within my CRM system and I need to find the upper and lower bounds of the half an hour surrorunding the timestamp at which somebody entered an event in the calendar in order to run some SQL on the DB to determine if they already have something booked in within that timeslot.

For example I have the timestamp of 1330518155 = 29 February 2012 16:22:35 GMT+4
so I need to get 1330516800 and 1330518600 which equal 16:00 and 16:30.

If anyone has any ideas or think I am approaching developing the calendar in a stupid way let me know! Its my first time on such a task involving so much work with times and dates so any advice appreciated!


Use modulo.

$prev = 1330518155 - (1330518155 % 1800);
$next = $prev + 1800;

The modulo operator gives the remainder part of division.