首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将日期时间修改为持续15分钟

将日期时间修改为持续15分钟
EN

Stack Overflow用户
提问于 2014-10-13 09:55:17
回答 2查看 3K关注 0票数 2

我有约会时间:

代码语言:javascript
复制
2014-08-05 13:27:00
2014-08-05 13:29:00 
2014-08-05 13:36:00 
2014-08-05 13:38:00

如何使用DateTime::modify()将其设置为

代码语言:javascript
复制
2014-08-05 13:15:00
2014-08-05 13:15:00 
2014-08-05 13:30:00 
2014-08-05 13:30:00

(所以,在最后15分钟..)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-10-13 10:15:26

当您请求它使用DateTime::modify()时,您可以这样做:

代码语言:javascript
复制
function roundToLastQuarterHour($date_string) {
    $date = new DateTime($date_string);

    // Remove any seconds, we don't need them
    $seconds = $date->format('s');
    $date->modify('-' . $seconds . ' seconds');

    // Store the original number of minutes on the datetime
    $original_minutes = $date->format('i');
    // Calculate how many minutes past the last quarter hour
    $remaining_minutes = $original_minutes - ($original_minutes - ($original_minutes % 15));

    // Modify the minutes, remove the number of minutes past the last quarter of an hour
    $date->modify('-' . $remaining_minutes . ' minutes');

    return $date;
}

roundToLastQuarterHour('2014-08-05 13:27:00')
// 2014-08-05 13:15:00
roundToLastQuarterHour('2014-08-05 13:29:00')
// 2014-08-05 13:15:00
roundToLastQuarterHour('2014-08-05 13:36:00')
// 2014-08-05 13:30:00
roundToLastQuarterHour('2014-08-05 13:38:00')
// 2014-08-05 13:30:00
票数 2
EN

Stack Overflow用户

发布于 2014-10-13 10:10:12

这就行了.

代码语言:javascript
复制
$adt = date("Y-m-d G:i:s",strtotime("2014-08-05 13:37:00")); // your desired time 
$min = date("i",strtotime("2014-08-05 13:37:00")); // taking the min out
// echo $adt;

if($min<15) $min =00; //changing the min value
elseif(15<$min && $min<30) $min =15;
elseif(30<$min && $min<45) $min =30;
elseif(45<$min && $min<=59) $min =45;

echo date("Y-m-d G:",strtotime($adt)).$min.date(":s",strtotime($adt)); //print or store it in a var

如果使用更多的时间值,也可以作为函数..。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26337205

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档