how to find number of days(days ago)different between today and database date ..

eg: difference between 04/09/2014 and 06/08/2014 =(no of days ago)

Dani AI

Generated

Brief summary and a robust approach that fills gaps left by earlier replies.

As asked for "days ago" and showed a timestamp hack, correctly warned about DST/leap-year edge cases and advocated DateTime. The missing pieces here are (1) avoid ambiguity in date formats (use ISO yyyy-mm-dd) and (2) normalize both dates to the same timezone/time-of-day so DST or partial-day times don't change the day count.

Example (safe, avoids DST issues and platform-format quirks):

$dbDateStr = '2014-04-09'; // DB value in YYYY-MM-DD
$dbDate = DateTimeImmutable::createFromFormat('Y-m-d', $dbDateStr, new DateTimeZone('UTC'))->setTime(0,0,0);
$today  = (new DateTimeImmutable('now', new DateTimeZone('UTC')))->setTime(0,0,0);

$interval = $dbDate->diff($today);
$days = $interval->days; // integer total days
if ($interval->invert) {
    echo $days . ' days ago';
} else {
    echo 'in ' . $days . ' days';
}

Notes and troubleshooting

  • Normalizing both objects to the same timezone (UTC here) and to midnight removes DST and partial-day surprises.
  • Using DateTimeImmutable prevents accidental mutation; DateInterval->days returns the total-day count (use it instead of formatting tricks that can vary by platform).
  • If the DB stores DATETIME/TIMESTAMP with no timezone, treat it explicitly (best practice: store UTC). For large result sets, compute the difference in SQL (e.g., DATEDIFF) for performance instead of fetching and diffing in PHP.

Recommended Answers

All 7 Replies

This is comparing two days scripts:

commented: this does not calculate the number fo days - you're just linking to weakly related articles on your own site. It's getting tedious :( -3
Member Avatar for Member #120589

^^ That will not do what you want. It doesn't even extract data from pre-formatted dates so that they can be used with mktime().

To date: 5 posts with 5 links to your site. What are the chances of you reaching 10 posts and 10 links, I wonder?

You can try

$endDate='2014-04-11';
$startdate='2014-04-01';
$daydiff = floor( ( strtotime( $endDate ) - strtotime( $startdate ) ) / 86400);

echo $daydiff;

it will count the day difference between the 2 dates

Member Avatar for Member #120589

^^ No it won't. Try running this on around the time of daylight saving or on a leap year and it will fail.

$endDate='2014-04-11';
$startdate='2014-03-31';
$daydiff = floor( ( strtotime( $endDate ) - strtotime( $startdate ) ) / 86400);
echo $daydiff;

$endDate='2014-04-11';
$startdate='2014-03-30';
$daydiff = floor( ( strtotime( $endDate ) - strtotime( $startdate ) ) / 86400);
echo $daydiff;

This will give you the same number of days on Europe/London Timezone due to the clocks going forward an hour on March 30.

Never use this to calculate differences.

I will refer you to the official link once again:

http://www.php.net/manual/en/datetime.diff.php

Hi Sorry about that its just that i am using it here in philippines and have no problem when it comes to leaf year or daylight. anyway base on the manual.

date_default_timezone_set('Asia/Manila');

$startdate=date_create("2014-03-30");
$endDate=date_create("2014-04-02");
$diff=date_diff($startdate,$endDate);

echo $diff->format("%a");
Member Avatar for Member #120589

Yep, that's the procedural version. Should do. However I remember a problem with running this on Windows a while back. I think it was to do with %a - I sort of remember you had to have PHP VC9 instead of VC6 installed. I may be wrong - it was a while ago.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.