program story

현재까지 'x'시간 추가

inputbox 2020. 11. 26. 08:14
반응형

현재까지 'x'시간 추가


현재 다음과 같이 현재 날짜 / 시간을 반환하는 PHP가 있습니다.

$now = date("Y-m-d H:m:s");

제가하고 싶은 것은 새로운 변수 $new_timeequal을 갖는 것입니다 $now + $hours. 여기서는 $hours24에서 800까지의 시간입니다.

어떤 제안?


strtotime()함수 와 같은 것을 사용 하여 현재 타임 스탬프에 무언가를 추가 할 수 있습니다. $new_time = date("Y-m-d H:i:s", strtotime('+5 hours')).

함수에 변수가 필요하면 큰 따옴표를 strtotime("+{$hours} hours")사용해야 strtotime(sprintf("+%d hours", $hours))합니다.


다른 솔루션 (객체 지향)은 DateTime :: add를 사용하는 것입니다.

예:

$now = new DateTime(); //current date/time
$now->add(new DateInterval("PT{$hours}H"));
$new_time = $now->format('Y-m-d H:i:s');

PHP 문서 .


이를 위해 strtotime ()사용할 수 있습니다 .

$new_time = date("Y-m-d H:i:s", strtotime('+3 hours', $now)); // $now + 3 hours

옳은

이를 위해 strtotime ()을 사용할 수 있습니다.

$new_time = date("Y-m-d H:i:s", strtotime('+3 hours', strtotime($now))); // $now + 3 hours

unix 스타일 시간을 사용하여 다음을 계산할 수도 있습니다.

$newtime = time() + ($hours * 60 * 60); // hours; 60 mins; 60secs
echo 'Now:       '. date('Y-m-d') ."\n";
echo 'Next Week: '. date('Y-m-d', $newtime) ."\n";

음 .. 분을 수정해야합니다 ... 'i'는 분입니다. 몇 달이 아닙니다. :) (나도 같은 문제가있었습니다.

$now = date("Y-m-d H:i:s");
$new_time = date("Y-m-d H:i:s", strtotime('+3 hours', $now)); // $now + 3 hours

lib Ouzo goodies를 시도 하고 유창하게 할 수 있습니다.

echo Clock::now()->plusHours($hours)->format("Y-m-d H:m:s");

API는 여러 작업을 허용합니다.


나는 이것을 사용하여 멋지게 작동합니다.

//set timezone
date_default_timezone_set('GMT');

//set an date and time to work with
$start = '2014-06-01 14:00:00';

//display the converted time
echo date('Y-m-d H:i',strtotime('+1 hour +20 minutes',strtotime($start)));

다음 함수를 사용하여 정상적인 날짜-시간 값을 mysql datetime 형식으로 변환합니다.

private function ampmtosql($ampmdate) {
            if($ampmdate == '')
                return '';
            $ampm = substr(trim(($ampmdate)), -2);
            $datetimesql = substr(trim(($ampmdate)), 0, -3);
            if ($ampm == 'pm') {
                $hours = substr(trim($datetimesql), -5, 2);
                if($hours != '12')
                    $datetimesql = date('Y-m-d H:i',strtotime('+12 hour',strtotime($datetimesql)));
            }
            elseif ($ampm == 'am') {
                $hours = substr(trim($datetimesql), -5, 2);
                if($hours == '12')
                    $datetimesql = date('Y-m-d H:i',strtotime('-12 hour',strtotime($datetimesql)));
            }
            return $datetimesql;
        }

datetime 값을 다음과 같이 변환합니다.

2015-06-04 09:55 AM -> 2015-06-04 09:55
2015-06-04 03:55 PM -> 2015-06-04 15:55
2015-06-04 12:30 AM -> 2015-06-04 00:55

이것이 누군가를 도울 수 있기를 바랍니다.


$date_to_be-added="2018-04-11 10:04:46";
$added_date=date("Y-m-d H:i:s",strtotime('+24 hours', strtotime($date_to_be)));

date ()strtotime () 함수 의 조합이 트릭을 수행합니다.


   $now = date("Y-m-d H:i:s");
   date("Y-m-d H:i:s", strtotime("+1 hours $now"));

참고 URL : https://stackoverflow.com/questions/11386308/add-x-number-of-hours-to-date

반응형