Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中将日期和时间转换为特定范围_Php - Fatal编程技术网

如何在PHP中将日期和时间转换为特定范围

如何在PHP中将日期和时间转换为特定范围,php,Php,例如,我有以下php日期: 2018-07-16 02:07:30 2018-07-16 02:17:30 2018-07-16 02:37:30 2018-07-16 02:47:30 我必须按语法将上述日期转换为以下日期: 2018-07-16 02:15:00 2018-07-16 02:30:00 2018-07-16 02:45:00 2018-07-16 03:00:00 e、 g如果分钟为1-15,则应为15;如果分钟为16-30,则应为30;如果分钟为31-45,则应为45;

例如,我有以下php日期:

2018-07-16 02:07:30
2018-07-16 02:17:30
2018-07-16 02:37:30
2018-07-16 02:47:30
我必须按语法将上述日期转换为以下日期:

2018-07-16 02:15:00
2018-07-16 02:30:00
2018-07-16 02:45:00
2018-07-16 03:00:00

e、 g如果分钟为1-15,则应为15;如果分钟为16-30,则应为30;如果分钟为31-45,则应为45;如果分钟为46-60,则应为00。

您可以使用格式方法。->

在那里,您可以找到参数,您可以使用这些参数访问要查找的日期的值。然后做一个if子句来检查并设置正确的日期。 也许制作一个数组以获得更好的结构

例如:

$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语句并编辑值,没有那么复杂! 我希望我能帮助你

首先,您应该从日期获取时间字符串,然后将分数设置为900秒,等于15分钟。然后,您可以通过$timestring_分数=$timestring%$fraction获得timestring_分数;然后计算分钟数,并通过执行此日期'Y-m-d H:i:s',$minutes创建一个新日期

代码

输出

最新答案 如果时间是这样的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,则时间分数也将为零,因此您需要在创建新日期之前检查时间分数,这是另一个需要优化的数组示例:

<?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;

?>
您可以轻松地将其转换为datetime对象。

请尝试以下代码:

$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;

您可以通过以下方式更改日期:

      $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;
请试一下


创建一个函数并将日期传递给该函数。函数将返回带有期望输出的通过日期

您已经尝试过解决编码问题了吗?我不知道。迭代每一行,从格式创建datetime,以新格式输出。非常基本。@SanjayChaudhari关于退出字符串或DateTime对象,您需要什么?DateTime例如2018-07-16 02:15:00这不是这个问题的正确答案。您的代码最终O/P数组4{[0]=>string19 2018-07-16 02:15:30[1]=>string19 2018-07-16 02:30[2]=>string19 2018-07-16 02:45:30[3]=>string19 2018-07-16 02:00:30//必须是2018-07-16 03:00:00}
$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");
$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;
      $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;