例如,我有以下php日期:
2018-07-16 02:15:00
2018-07-16 02:30:00
2018-07-16 02:45:00
2018-07-16 03:00:00
我必须按照语法将以上日期转换为以下日期:
QQuickPaintedItem
例如,如果分钟为1-15,则应为15,如果分钟为16-30,则应为30,如果分钟为31-45,则应为45,如果分钟为46-60,则应为00。 / p>
答案 0 :(得分:2)
首先,您应该从日期中获取timestring
,然后将分数设置为900(秒),等于15分钟。您可以Convert 900 Seconds to Minutes,然后通过timestring_fraction
得到$timestring_fraction = $timestring % $fraction;
,并通过执行date('Y-m-d H:i:s', $minutes)
来计算分钟数并创建新日期。
代码
$date = '2018-07-16 02:37:30';
$timestring = strtotime($date);
// 900 seconds which is equal to 15 minutes
$fraction = 900;
$timestring_fraction = $timestring % $fraction;
$minutes = $timestring + ($fraction - $timestring_fraction);
$updated_date = date('Y-m-d H:i:s', $minutes);
echo $updated_date;
输出
// 2018-07-16 02:07:30
2018-07-16 02:15:00
// 2018-07-16 02:17:30
2018-07-16 02:30:00
// 2018-07-16 02:37:30
2018-07-16 02:45:00
// 2018-07-16 02:47:30
2018-07-16 03:00:00
如果时间是这样的2018-07-16 02:30:00
,那么解决方案将是
$timestring_fraction = $timestring % $fraction;
if ($timestring_fraction === 0) {
echo $date;
}
else {
$minutes = $timestring + ($fraction - $timestring_fraction);
$updated_date = date('Y-m-d H:i:s', $minutes);
echo $updated_date;
}
在这种情况下,如果分钟数完全相同,即15、30、45或00,则时间比例也将为零,因此您需要在确定新日期之前检查时间比例
答案 1 :(得分:1)
请尝试
$date = "2018-07-16 02:48:30";
$cons = date('Y-m-d H:',strtotime($date));
$min = date('i',strtotime($date));
$min = $min + 15 - ($min % 15) ;
if ($min == 60){
$min = '00';
$final = date('Y-m-d H:i:s',strtotime($cons.$min));
$final = date('Y-m-d H:i:s', strtotime($final.'1 hour'));
}else{
$final = date('Y-m-d H:i:s',strtotime($cons.$min));
}
echo $final;
创建一个函数并将日期传递给该函数。函数将返回带有您期望输出的通过日期。
答案 2 :(得分:0)
您可以使用format()方法。 -> http://php.net/manual/de/function.date.php
您可以在其中找到参数,使用这些参数可以访问要查找的日期的值。然后,如果有子句,请检查并正确设置日期。 也许做一个数组以获得更好的结构!
例如:
$day = date_create("2018-07-16 02:15:00");
$dates = array($day); // And so on...
echo $dates[1]->format("H");
// You get the hour in 24h format with like --> 02
// with i you get the minute like --> 15
比您创建一个switch语句或if语句并编辑值,没有那么复杂! 希望我能为您服务!
答案 3 :(得分:0)
另一个需要优化的数组示例:
<?php
$array = [
'2018-07-16 02:07:30',
'2018-07-16 02:17:30',
'2018-07-16 02:37:30',
'2018-07-16 02:47:30'
];
$newArray = [];
foreach ($array as $elem) {
$time = explode(' ', $elem)[1];
$date = explode(' ', $elem)[0];
$timePart = explode(':', $time);
$minutes = $timePart[1];
if ($minutes > 1 && $minutes < 15) {
$newTime = $date . ' ' . $timePart[0] . ':15:' . $timePart[2];
} else if ($minutes > 15 && $minutes < 30) {
$newTime = $date . ' ' . $timePart[0] . ':30:' . $timePart[2];
} else if ($minutes > 30 && $minutes < 45) {
$newTime = $date . ' ' . $timePart[0] . ':45:' . $timePart[2];
} else if ($minutes > 45) {
$newTime = $date . ' ' . $timePart[0] . ':00:' . $timePart[2];
}
$newArray[] = $newTime;
}
echo "<pre>"; var_dump($newArray); exit;
?>
U可以轻松地将其转换为日期时间对象。
答案 4 :(得分:0)
尝试以下代码:
$dateString = '2018-07-16 02:47:30';
$date = new DateTime($dateString);
$minute = $date->format('i');
switch(true) {
case $minute <= 15:
$minute = 15;
break;
case $minute <= 30:
$minute = 30;
break;
case $minute <= 45:
$minute = 45;
break;
case $minute <= 60:
$minute = 60;
break;
}
$date->setTime($date->format("H"), $minute);
echo $date->format("Y-m-d H:i:s");
答案 5 :(得分:0)
您可以通过以下方式更改日期:
$date = '2018-07-16 02:07:30';
$justdate = date('Y-m-d', strtotime($date));
$justdate = new DateTime($justdate);
$sdate = date('s', strtotime($date));
$idate = date('i', strtotime($date));
$hdate = date('h', strtotime($date));
$idate = number_format($idate);
if ($idate >= 0 && $idate <= 15) {
$idate = '15';
$justdate->setTime($hdate, $idate, $sdate);
$justdate = $justdate->format('Y-m-d H:i:s');
} else if ($idate >= 16 && $idate <= 30) {
$idate = '30';
$justdate->setTime($hdate, $idate, $sdate);
$justdate = $justdate->format('Y-m-d H:i:s');
} else {
$hdate = $hdate + 1;
$idate = '00';
$sdate = '00';
$justdate->setTime($hdate, $idate, $sdate);
$justdate = $justdate->format('Y-m-d H:i:s');
}
echo $justdate;exit;