Ash*_*Ash 20 php datetime unix-timestamp
好的,我正在我的CRM系统中处理一个日历应用程序,我需要找到半小时的上限和下限超过某人在日历中输入事件的时间戳,以便在数据库上运行一些SQL确定他们是否已在该时段内预订了某些内容.
例如,我的时间戳为1330518155 = 2012年2月29日16:22:35 GMT + 4所以我需要得到1330516800和1330518600等于16:00和16:30.
如果有人有任何想法或认为我正在以愚蠢的方式开发日历,请告诉我!这是我第一次参与这么多涉及时间和日期工作的任务,所以任何建议都值得赞赏!
小智 66
使用模数.
$prev = 1330518155 - (1330518155 % 1800);
$next = $prev + 1800;
Run Code Online (Sandbox Code Playgroud)
模运算符给出除法的其余部分.
我没有清楚地阅读问题,但是对于那些不需要两者之间范围的人,此代码将四舍五入到最接近的半小时。使用一些 SenorAmor 的代码。道具和他对正确问题的疯狂优雅解决方案。
$time = 1330518155; //Or whatever your time is in unix timestamp
//Store how many seconds long our rounding interval is
//1800 equals one half hour
//Change this to whatever interval to round by
$INTERVAL_SECONDS = 1800; //30*60
//Find how far off the prior interval we are
$offset = ($time % $INTERVAL_SECONDS);
//Removing this offset takes us to the "round down" half hour
$rounded = $time - $offset;
//Now add the full interval if we should have rounded up
if($offset > ($INTERVAL_SECONDS/2)){
$nearestInterval = $rounded + $INTERVAL_SECONDS;
}
else{
$nearestInterval = $rounded
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
29755 次 |
| 最近记录: |