Rails 3 get days between two MySQL dates - mysql

I want to be able to work out the number of days between two dates. One date is the current date, the other is from a database of events.
So far, I have tried this in my Model:
this_date = DateTime.parse(self.date.to_s)
the_date = Time.now
between = (this_date.to_i - the_date.to_i)
Which was a suggestion from another question (well, kinda).
The date format from the database is 'YYYY/mm/dd', and the date from Time.now is a lot different which is what I cannot work out.
Any help would be appreciated.
Thanks

What data type is the db column for self.date in your model? I note you're casting it to a string so what was it originally? If it's a datetime type then you can skip the DateTime.parse and just compare the two dates directly to get the difference in seconds:
difference = self.date - Time.now
The fun part is then converting that difference to usable values. This question has an excellent answer to that.
Edit: for a Date column you can do:
difference = (self.date - Date.today).to_i
Which will give you a difference in whole days.

this_date = Date.parse self.date
remaing_days = (Date.today - this_date)

select (unix_timestamp(first_date)-unix_timestamp(second_date))/84600 from table
84600 is seconds in one day
unix_timestamp is date as seconds since 1970-01-01
So you don't need Ruby :-)

Even though it is answered, but I am posting my solution to this problem, that helped me to solve my problem. I have used:
Time.zone.now > e.start && Time.zone.now < e.end + 1.day

Related

Unable to get Results of a query

I am newbie to SQL.
I have this query
I am not sure where i am doing it wrong.
Can you guys help me out?
thanks.
Three mistakes:
You need single quotes
MySql expects two-digit months
The 2015-09-30 end date includes an implicit midnight for the time component, which excludes most of the day
Put it all together you get this:
SELECT * FROM results WHERE played_on BETWEEN '2015-09-01' AND '2015-10-01';
While I'm here, I prefer to avoid BETWEEN in favor of explicit bounds. There's always that chance someone codes a game for exactly midnight October 1:
SELECT * FROM results WHERE played_on >= '2015-09-01' AND played_on < '2015-10-01';
And you would have had this answer faster if I could have copy/pasted the query text from your post instead of having to re-type. Posting images of sample data or code instead of the text is considered very rude here.
i think you can use MySql Date Function like MONTH() and YEAR() for this query :
SELECT * FROM results WHERE MONTH(played_on) = 9 AND YEAR(played_on) = 2015
I hope this answer can help you.

Mysql Timestamp and Var

I have I'm having to workout the days difference between 2 columns one of which is a timestamp and the other being a varchar
timestamp - 2016-01-25 23:55:23 and varchar - 24/12/2015
not the best format for dates, but given that I'm unable to change the columns type is it possible to work out the difference in days between those 2 columns?
Many thanks
Max
The DateTime Class in PHP is very powerful and flexible, specially using the createFromFormat() method to load dates of different formats quite happily. Used together with the ->diff() method you should get what you want.
Here is an example
<?php
$ts = '2016-01-25 23:55:23';
$vc = '24/12/2015';
$tsdate = DateTime::createFromFormat('Y-m-d H:i:s', $ts);
$vcdate = DateTime::createFromFormat('d/m/Y', $vc);
$interval = $vcdate->diff($tsdate);
echo $interval->format('%R%a days');
Alternatively you could get MYSQL to do the heavy lifting and use its STR_TO_DATE() function when you query this odd varchar field
SELECT tsCol, STR_TO_DATE(varcharColumn, '%d/%m/%Y) as otherDate
Then when you see it in PHP it will be converted to a MySQL DATETIME column
Or going even further and getting MySQL to do all the work you could use MySQL's TIMESTAMPDIFF
SELECT TIMESTAMPDIFF(DAY, tsCol, STR_TO_DATE(varcharColumn, '%d/%m/%Y)) as differenceInDays;

Cron job making date database entry

I have a very simple code for a cron job that makes a date entry into an SQL DB:
$qry_cron_test = "INSERT INTO ".$tblprefix."cron_test SET
create_datetime = '".date("Y-d-m H:i:s")."'";
$rs_cron_test = $db -> Execute($qry_cron_test);
The problem is the following:
Between 1st and 12th of every month the date entry is like this - 2014-10-03 07:30:39, which is what i want.
However, when the current date is between 13th and the end of the month, the date entry looks like this - 0000-00-00 00:00:00. Then when 1st comes the entires are all ok again.
I tested this on couple of servers and also locally on Xampp always with the same result.
Any suggestions? What could be possibly wrong?
You have month and day the wrong way around.
$qry_cron_test = "INSERT INTO ".$tblprefix."cron_test SET
create_datetime = '".date("Y-m-d H:i:s")."'";
$rs_cron_test = $db -> Execute($qry_cron_test);
date("Y-m-d H:i:s")
I recommend that, unless you need milisecond information, you always store date information in Unix Timestamp. It is lighter to store, since it is only a integer value, is faster to retrieve and is universal, since it is always based on UTC.
Specially in PHP, converting date information to (time() and strtotime) and from (date()) a unix timestamp is pretty easy. This way no matter where your user is, you can always show correct information in local time with almost no effort.
Wouldn't it be simpler to just do this:
insert into cron_test
create_datetime
values
(current_timestamp)

Convert MySQL date and time to datetime

I have a table which has 4 columns. Date login, date logout, time login, time logout.
How can I get the difference. I tried something like this;
SUM(TIMESTAMP(date_logout, time_logout) - TIMESTAMP(date_log, time_log))
However I'm getting a really really high number and I am unable to convert it to proper time. Any hints or clues? I didn't design the table btw :P
Thanks in advance!
if you want the diff in secounds:
SUM(UNIX_TIMESTAMP(TIMESTAMP(date_logout, time_logout)) - UNIX_TIMESTAMP(TIMESTAMP(date_log, time_log)))
If you want the diff in days:
SUM(DATEDIFF(TIMESTAMP(date_logout, time_logout), TIMESTAMP(date_log, time_log)))
If you want the diff as HH:mm:ss
SUM(TIMEDIFF(TIMESTAMP(date_logout, time_logout), TIMESTAMP(date_log, time_log)))
Subtract the two value by using TIMEDIFF FUNCTION.
TIMEDIFF(TIMESTAMP(date_logout, time_logout) ,TIMESTAMP(date_log, time_log) )
TIMEDIFF(TIMESTAMP(date_logout, time_logout) , TIMESTAMP(date_log, time_log))

mysql calculation

This is my initial question....well I got a response but I'm still stuck.
Can anyone please explain how can I achieve this in mysql:
I have two fields in mysql, 'cap_commdate' with DATE TYPE and 'cap_policyterm' with INT TYPE. I want to have another field called 'cap_maturityDate' which will automatically compute the policy term pereod in years (in layman's term ie: cap_commdate*cap_policyterm). What is the right SQL query to use; or what is the best approach; and I want to use it in my recordset to prepare a confirmation page... please a simple explanation...
I have tried the following:
SELECT DATE_ADD(cap_commdate, INTERVAL cap_policyterm YEAR) ...
I ran the query and got errors; so i edited it and used:
SELECT
DATE_ADD("cap_commdate", INTERVAL "cap_policyterm" YEAR) AS cap_maturity FROM capital
All I got was empty fields. Please help out.
It sounds like you want this:
SELECT DATE_ADD(cap_commdate, INTERVAL cap_policyterm YEAR)
...
This DATE_ADD function adds the given number of years to the starting date, thus producing the maturity date — just as you've described.
Please show the errors you get. The query
SELECT DATE_ADD(cap_commdate, INTERVAL cap_policyterm YEAR) FROM capital
should work.