RibAs
RibAs

Reputation: 11

Find TimeDiff between two dates

I have a MySQL database with some columns and in two of them I have two different dates.

In the site I have the two dates in a two colums inside a table, but I need to calculate and reproduce in another column the number of days between that two dates.

I found this code:

?php
$date1 = date_create("2017-04-15");
$date2 = date_create("2017-05-18");

//difference between two dates
$diff = date_diff($date1,$date2);

//count days
echo 'Days Count - '.$diff->format("%a");
?

And it works but I need to change this dates and put the data inside my database. Jow can I solve this?

Im using this:

update client_invoices set x6 = datediff(x4, date_due)

as an event in mysql but everytime they updates the server the "Event scheduler status" is turned off.

How can i perform this directly in my sql without scheduling events?

Thanks, Ribas

Upvotes: 0

Views: 138

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269513

In MySQL, you would simply use datediff():

select t.*, datediff(day1, day2) as days_diff
from t;

If you want to update a column:

update t
    set diff = datediff(day1, day2);

Upvotes: 2

Related Questions