Jump to content

Calculate days from two dates


684425

Recommended Posts

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.