Avatar of syedasimmeesaq
syedasimmeesaq
Flag 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
PHP

Avatar of undefined
Last Comment
syedasimmeesaq

8/22/2022 - Mon
Robin

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
Mahdii7

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
syedasimmeesaq

ASKER
Works!
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck