-1

Below is the output from a database and I am trying to get the number of months between the start date and the date from the database. Below is my output

string(10) "12/12/2010" Date : 12/12/2010 - 1292112000
string(10) "19/04/2000" Date : 19/04/2000 -
string(10) "08/08/2007" Date : 08/08/2007 - 1186527600
string(10) "20/06/2011" Date : 20/06/2011 - 

As you can see, the second and fourth unix timestamps are empty. I don't know what could be causing the issue. Below is the code I'm using to echo what you see above.

echo "Date : " . $row['startDate'] . " - " . strtotime($row['startDate']) . var_dump($row['startDate']). "<br />";
roadkill247
  • 213
  • 1
  • 5
  • 12
  • a / separator is US format, and 20/06 6th day of 20th month is an invalid date.... use - separator for UK dates - http://www.php.net/manual/en/datetime.formats.date.php – Mark Baker Mar 28 '13 at 17:02

2 Answers2

9

From the manual:

Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.

To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.

John Conde
  • 217,595
  • 99
  • 455
  • 496
1

Solved the issue, I used some of the new DateTime functions in 5.3 PHP

$originalDate = str_replace("/", "-", $row['startDate']);
$date1 = new DateTime($originalDate);
$date2 = new DateTime('now');

$interval = date_diff($date1, $date2);

echo "Months" . $interval->format('%m') . ($interval->format('%y') * 12) . "<br />";
roadkill247
  • 213
  • 1
  • 5
  • 12