Programing

날짜에 하루 추가

lottogame 2020. 6. 9. 07:36
반응형

날짜에 하루 추가


날짜에 하루를 추가하는 코드는 추가하기 전에 날짜를 반환합니다. 하루를 추가 2009-09-30 20:24:00한 후 날짜 다음 달로 롤오버해야합니다.1970-01-01 17:33:29

<?php

    //add day to date test for month roll over

    $stop_date = date('Y-m-d H:i:s', strtotime("2009-09-30 20:24:00"));

    echo 'date before day adding: '.$stop_date; 

    $stop_date = date('Y-m-d H:i:s', strtotime('+1 day', $stop_date));

    echo ' date after adding one day. SHOULD be rolled over to the next month: '.$stop_date;
?>

전에 비슷한 코드를 사용했는데, 여기서 내가 뭘 잘못하고 있습니까?


<?php
$stop_date = '2009-09-30 20:24:00';
echo 'date before day adding: ' . $stop_date; 
$stop_date = date('Y-m-d H:i:s', strtotime($stop_date . ' +1 day'));
echo 'date after adding 1 day: ' . $stop_date;
?>

PHP 5.2.0+의 경우 다음과 같이 할 수도 있습니다.

$stop_date = new DateTime('2009-09-30 20:24:00');
echo 'date before day adding: ' . $stop_date->format('Y-m-d H:i:s'); 
$stop_date->modify('+1 day');
echo 'date after adding 1 day: ' . $stop_date->format('Y-m-d H:i:s');

$date = new DateTime('2000-12-31');

$date->modify('+1 day');
echo $date->format('Y-m-d') . "\n";

가장 간단한 해결책 :

$date = new DateTime('+1 day');
echo $date->format('Y-m-d H:i:s');

그것은 나를 위해 일했다 : 현재 날짜

$date = date('Y-m-d', strtotime("+1 day"));

언제라도 :

date('Y-m-d', strtotime("+1 day", strtotime($date)));

이 시도

echo date('Y-m-d H:i:s',date(strtotime("+1 day", strtotime("2009-09-30 20:24:00"))));

읽고 이해하기 쉬운 방법 :

$original_date = "2009-09-29";

$time_original = strtotime($original_date);
$time_add      = $time_original + (3600*24); //add seconds of one day

$new_date      = date("Y-m-d", $time_add);

echo $new_date;

나는 항상 86400 (하루에 초)을 추가합니다.

$stop_date = date('Y-m-d H:i:s', strtotime("2009-09-30 20:24:00") + 86400);

echo 'date after adding 1 day: '.$stop_date; 

아마 당신이 할 수있는 가장 매끄러운 방법은 아니지만 작동합니다!


Doug Hays의 답변에 동의하지만 코드가 작동하지 않는 이유는 strtotime () 이 문자열이 아닌 두 번째 인수로 INT를 기대 하기 때문입니다 ( 날짜를 나타내는 것조차도).

If you turn on max error reporting you'll see this as a "A non well formed numeric value" error which is E_NOTICE level.


The modify() method that can be used to add increments to an existing DateTime value.

Create a new DateTime object with the current date and time:

$due_dt = new DateTime();

Once you have the DateTime object, you can manipulate its value by adding or subtracting time periods:

$due_dt->modify('+1 day');

You can read more on the PHP Manual.


The following code get the first day of January of current year (but it can be a another date) and add 365 days to that day (but it can be N number of days) using DateTime class and its method modify() and format():

echo (new DateTime((new DateTime())->modify('first day of January this year')->format('Y-m-d')))->modify('+365 days')->format('Y-m-d');

참고URL : https://stackoverflow.com/questions/1394791/adding-one-day-to-a-date

반응형