1

I have a string in the format YYYYMMDDHH24MISS that is year, month, day, hours, minutes, seconds. I want to convert this to a date, add one day to it and return it in the same format. Sounds simple but I am unable to get this to work. I have tried a number of different ways where $field3 contains the date string for example:

                $end_date = strtotime(substr($field3,1,8));
                $date_interval = DateInterval::createFromDateString('1 day');
                $new_end_date = date_add($end_date, $date_interval);
                $field3 = ($new_end_date->format('YYYYMMDD')).substr($field3,8,6);

In this example $new_end_date contains "false".

Example date time string: 20170912124159 being 12/09/2017 12:41:59

Sarah
  • 357
  • 4
  • 14
  • 1
    What is the meaning of `24MI` in the date format? Update the question with some examples of input strings and their corresponding dates. – axiac Sep 12 '17 at 08:04
  • Please have a look at [this](https://stackoverflow.com/questions/1394791/adding-one-day-to-a-date). This shows very easy ways to add a day to a date – SacrumDeus Sep 12 '17 at 08:07
  • I wouldn't rely on strtotime. You can control formatting directly with: $end_date = DateTime::createFromFormat('Ymd', substr($field3,0,8)); – Yosh Sep 12 '17 at 08:12
  • `YYYYMMDDHH24MISS` looks like an Oracle format code, equivalent to PHP's `YmdHis`. You appear to be trying to discard the time part but then say you want it there. Could you please edit the question and clarify that? – Álvaro González Sep 12 '17 at 08:53

3 Answers3

1

The format of your input string can be parsed by the constructor of class DateTime (and date_create() and strtotime()) without problems.

$date = new DateTime('20170912124159');
$date->add(new DateInterval('P1D'));
echo($date->format('Y-m-d H:i:s'));

# The output is:
# 2017-09-13 12:41:59

You can, as well, format the date as string using the format YmdHis to get the modified date in the same format as the input string.

echo($date->format('YmdHis'));
# 20170913124159

Read about DateTime and DateInterval.

axiac
  • 68,258
  • 9
  • 99
  • 134
0

You can try something like this:

$date = DateTime::createFromFormat('YmdHis', '20170912131313');
$date->add(new DateInterval('P10D'));
echo $date->format('Y-m-d');

For more information: http://php.net/manual/en/datetime.add.php

Suresh Kamrushi
  • 15,627
  • 13
  • 75
  • 90
-1

Please try this

$date = date("Y/m/d H:i:s");   // or  '2017/09/30 20:24:00'
$ndate = date('Y/m/d H:i:s', strtotime($date . ' +1 day'));
echo 'date after adding 1 day: ' . $ndate;
Naincy
  • 2,953
  • 1
  • 12
  • 21
  • why negative ? is It not working for you? or please correct – Naincy Sep 12 '17 at 08:11
  • How is processed the input string in your code? [`date()`](http://php.net/manual/en/function.date.php) formats a date as string, the OP already has strings and want to convert them back to dates in order to process them. With only one argument `date()` formats the current time; the question doesn't care about the current time. Aren't these reasons enough to downvote? – axiac Sep 12 '17 at 08:20
  • @axiac in place of date you can give string also in the format it will work. See my updated answer. – Naincy Sep 12 '17 at 08:23
  • You didn't get it. The question asks how to add 1 day to some dates represented in a format that **is not** `Y/m/d H:i:s` then format the updated dates in the same format. Your answer uses the wrong format and it doesn't care about the input strings. It always formats the current time +1 day. How do you think this is useful to the OP? – axiac Sep 12 '17 at 08:26