0

I want to calculate the remaining duration using an SQL query or using PHP. I have a table that has fields
- date_submit (example 2019-12-01 09:40:22 ) and
- duration ( example 30 days )

For example today is 2019-12-02 so the remaining duration is 29, how can I calculate this in SQL or PHP?

Nick
  • 138,499
  • 22
  • 57
  • 95
Didik Ariyana
  • 39
  • 3
  • 8
  • Why do you need to store the remaining duration while you already have the starting date? – Arcesilas Nov 30 '19 at 23:55
  • it's like time limit, so i should know the remaining duration – Didik Ariyana Nov 30 '19 at 23:56
  • I mean these two pieces of data are redundant: if you store the start date (and you know the end date), you don't need to **store** the duration. You need to calculate it to display it, to check if it's not too late, or whatever, but not to store it. – Arcesilas Nov 30 '19 at 23:58
  • Does this answer your question? [How to get the number of days of difference between two dates on mysql?](https://stackoverflow.com/questions/2490173/how-to-get-the-number-of-days-of-difference-between-two-dates-on-mysql) – challet Dec 01 '19 at 00:03

1 Answers1

1

In your SQL query, you can use TIMESTAMPDIFF to compute the remaining duration:

SELECT TIMESTAMPDIFF(DAY, CURDATE(), DATE(date_submit) + INTERVAL duration DAY) AS remaining
Nick
  • 138,499
  • 22
  • 57
  • 95