Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 806
Ash Avatar asked Mar 09 '12 19:03

Ash


People also ask

How do you round a timestamp to an hour?

Round time to nearest hour ( TIME(1,0,0) = 1/24 representing an hour) and add a zero time value to ensure the expression is cast as a Time. M: There isn't an equivalent of MROUND in M, so instead multiply the time by 24 to express it as a number of hours, then round, then divide by 24 and convert to a Time type.


1 Answers

Use modulo.

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

The modulo operator gives the remainder part of division.

like image 113
SenorAmor Avatar answered Sep 19 '22 02:09

SenorAmor