Jump to content
684425

Calculate days from two dates

Recommended Posts

Posted (edited)

I have date stored in database in any of the given forms 2020-06-01, 2020-05-01 or 2019-04-01

I want to compare the old date with current date 2020-06-14

And the result should be in days.

Any help please?

PS: I want to do it on php side. but if its possible to do on database side (I am using myslq) please share both ways🙂

Edited by 684425

Share this post


Link to post
Share on other sites

Assuming PHP 5.3 or better and db date is in the format Y-m-d.

$fromdb=new DateTime($datefromdb);
$today=new DateTime();
$diff=$fromdb-$today;
print("Difference is ".$diff.d." days);

Note the result can be negative days.

  • Like 1

Share this post


Link to post
Share on other sites
Posted (edited)

Use $diff->days.

$dt1 = new DateTime('2020-05-01');
$diff = $dt1->diff(new DateTime())->d;       //--> 14;
$diff = $dt1->diff(new DateTime())->days;    //--> 44;

->d gives the days as in "1 month 14 days"

->days gives the total days

Using SQL:

select datediff(curdate(), '2020-05-01') as days;
+------+
| days |
+------+
|   44 |
+------+

 

Edited by Barand
  • Like 2

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.