-2

So I have a table in database and one of its attribute its a date ex. startdate=2020-08-12 I took this startdate from database and I want to add 6 months to it as enddate. But idk how to make it be considered as a date on php.

Altina
  • 1
  • 2
  • 1
    So if your database holds a date like `12.08.2020` then it must be a char/varchar. That means you have instantly lost the simple use of many Date manipulation functions. – RiggsFolly Dec 15 '20 at 16:20
  • Is there any PHP involved? Or is this all going on in Javascript – RiggsFolly Dec 15 '20 at 16:21
  • 1
    Welcome, to improve your experience on SO please read [how to ask](https://stackoverflow.com/help/how-to-ask), an [On Topic question](https://stackoverflow.com/help/on-topic), then look at the [Question Check list](https://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist), the [perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/), how to create a [Minimal, Complete and Verifiable Example](http://stackoverflow.com/help/mcve) and [take the tour](http://stackoverflow.com/tour) – RiggsFolly Dec 15 '20 at 16:21
  • @RiggsFolly its the date type 2020-08-12 I just wrote it like that, and yes its php – Altina Dec 15 '20 at 16:25
  • 1
    If it's really a date field, there are multiple ways of handling this (directly in MySQL, in PHP and in JS), all of them readily available both on and off this site. – El_Vanja Dec 15 '20 at 16:33
  • @El_Vanja thats it thank you so much – Altina Dec 15 '20 at 17:10

2 Answers2

1

function getFutureDate(date) {
   let d = new Date(date);
   d.setMonth(d.getMonth() + 6);
   return d.toDateString();
}
console.log(getFutureDate("2020-08-12"));

JustRaman
  • 1,101
  • 9
  • 11
0

For php, You may use mktime

<?php
$startdate='2020-08-12';

$sixmonthlater  = mktime(0, 0, 0, date("m",strtotime($startdate))+6  , date("d",strtotime($startdate)), date("Y",strtotime($startdate)));

// show unix time

echo $sixmonthlater;
echo "<br>";

// show time in Y-m-d

echo date("Y-m-d", $sixmonthlater);

?>
Ken Lee
  • 6,985
  • 3
  • 10
  • 29