0

Having problems adding 1 month to a variable from a database trying numerous methods given on the forum:

$row["pchargedate"] returns 2018-03-01

$newduedate = date('Y-m-d', strtotime('+1 month', $row["pchargedate"])) ;
-returns 0000-00-00

$newduedate = date('Y-m-d', strtotime('+1 month', $row["pchargedate"])) ; }
$newduedate = date ( 'Y-m-d' , $newduedate );
- returns 1970-01-01

$duedate = DateTime::createFromFormat('Y-m-d', $row["pchargedate"]);
$duedate = $duedate->format('d-m-Y');

if ($row["pchargedate"] == '1' ) { $newduedate = strtotime ( '+1 month' , strtotime ( $pchargedate ) ) ; }
$newduedate = date ( 'Y-m-d' , $newduedate );
- $duedate returns 01-03-2018 but $newduedate is 1970-01-01

Any help much appreciated.

TarangP
  • 2,711
  • 5
  • 20
  • 41

1 Answers1

1

strtotime — Parse about any English textual datetime description into a Unix timestamp

The Syntex is

int strtotime ( string $time [, int $now = time() ] )

What about this

<?php

$row = "2018-03-01";
echo date('Y-m-d', strtotime($row. ' + 1 month'));
//Outputs 2018-04-01

And the syntex you are using is

$newduedate = date('Y-m-d', strtotime('+1 month', $row["pchargedate"])) ;

Which is wrong. so it will output by default date

Kindly read This Documentation.

TarangP
  • 2,711
  • 5
  • 20
  • 41
  • *How* does this solve their problem? You should explain what they are doing wrong and why this is the solution to their problem. – John Conde Mar 09 '18 at 12:58
  • Sure @JohnConde – TarangP Mar 09 '18 at 13:00
  • Are you sure That `$row["pchargedate"]` returns `2018-03-01`. and this will usually happen when the datatype is set to date and you pass incorrect value so it will saved as 0000-00-00 so please check that . – TarangP Mar 09 '18 at 13:16
  • I have put 'echo $....' at various points in the code flow to check what date is being output to try and work out what's wrong. $pchargedate is always a date - either YYYY-mm-dd or dd-mm-YYYY depending on what I tried, same with $duedate. The problem is getting the proper $newduedate value return after trying to add a month. – user3691363 Mar 09 '18 at 13:18
  • I have just tried:----- if ($row["pchargedate"] == '12' ) { $newduedate = date('Y-m-d', strtotime($pchargedate. ' + 1 year')) ; } $newduedate = date ( 'Y-m-d' , $newduedate ); ---- and it returns 1970-01-01. If I just use the if statement I get 0000-00-00 – user3691363 Mar 09 '18 at 13:22