Link to home
Create AccountLog in
Avatar of syedasimmeesaq
syedasimmeesaqFlag for United States of America

asked on

date problem

I am using this to count the days till it expires

function dateDiff($dformat, $endDate, $beginDate)
{
$date_parts1=explode($dformat, $beginDate);
$date_parts2=explode($dformat, $endDate);
$start_date=gregoriantojd($date_parts1[0], $date_parts1[1], $date_parts1[2]);
$end_date=gregoriantojd($date_parts2[0], $date_parts2[1], $date_parts2[2]);
return $end_date - $start_date;
}






$expiry_date = "07-11-2106";
$today = date('m-d-Y');

echo dateDiff("-",$expiry_date,$today); // # of days
echo "<br />";

which works fine. But if I change the $expiry_date = "2106-07-11" and $today = date('Y-m-d') it stops working. I need it to be in Y-m-d formate ..what can I do. Thanks
Avatar of Robin Uijt
Robin Uijt
Flag of Netherlands image

gregoriantojd() is expecting these parameters:

int gregoriantojd ( int $month , int $day , int $year )

so change the function to the one below and it will work in the other format:


function dateDiff($dformat, $endDate, $beginDate)
{
  $date_parts1=explode($dformat, $beginDate);
  $date_parts2=explode($dformat, $endDate);
  $start_date=gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]);
  $end_date=gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]);
  return $end_date - $start_date;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mahdii7
Mahdii7
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of syedasimmeesaq

ASKER

Works!